Code2care@Mac % python3 zip_program.py
Traceback (most recent call last):
File "/Users/c2ctech/Desktop/zip_program.py", line 6, in
with zipfile.ZipFile(zip_file, 'r') as zip_ref:
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/homebrew/Cellar/python@3.11/3.11.4_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/zipfile.py", line 1302, in __init__
self._RealGetContents()
File "/opt/homebrew/Cellar/python@3.11/3.11.4_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/zipfile.py", line 1369, in _RealGetContents
raise BadZipFile("File is not a zip file")
zipfile.BadZipFile: File is not a zip file
You will get a BadZipFile error when you try to unzip a zip file with is either corrupt or not a valid zip file.
Fix:
Try the below options for a fix.
- Make sure that the file you are trying to unzip has a .zip extension.
- If you had downloaded it from the web or a cloud drive, make sure that it was fully downloaded (do a checksum or size check)
- Make sure that the path and the file name provided in your code is correct.
- Try to unzip the file by yourself and see if it works.
When using the zipfile module it is always recommended to wrap your code with try/catch block with and handle zipfile.BadZipFile
import zipfile
zip_file = "data.zip"
extract_fonder = "/user/c2c/prod"
try:
with zipfile.ZipFile(zip_file, 'r') as zip:
zip.extractall(extract_fonder)
print(f"The file {zip_file} was extracted successful!")
except zipfile.BadZipFile:
print(f"An Error occurred while extracting {zip_file} is not a valid ZIP file.")
Referece: https://docs.python.org/3/library/zipfile.html#zipfile.BadZipFile
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Python,
- Python List of Lists with Examples
- Fix: ERROR: Could not find a version that satisfies the requirement tkinter (from versions: none) ERROR: No matching distribution found for tkinter
- How to Convert Python Notebook .ipynb (JSON) to Executable .py Module
- ModuleNotFoundError: No module named qdarkstyle.colorsystem [Python]
- Python: Pandas Merge Indicator (Left, Right and Both) Example
- Fix: EOFError Exception in Python
- How to URL Decode a Query String in Python
- How to take user input from the console in a Python program
- Compare two lists in Python and return matches
- How to change the Python Default version
- Fix: KeyError: exception in Python
- How to get the Execution Time of A Python Program
- Fix: ModuleNotFoundError: No module named boto3 [Python]
- How to get unique values from a list in Python
- How to pip install Python Modules in VSCode
- Python: Fix - TypeError: NoneType object is not iterable
- How to delete a file using Python code example
- How to create a dictionary comprehension in Python
- Python: Get just the filename without extension using Path
- How to comment out a block of code in Python
- How to add two float numbers in Python
- Python: How to POST Json Data with HTTP Request
- Get MD5 Hash as Checksum of a String in Python
- Python - Convert float to String
- How to Parse XML String in Python
More Posts:
- Online Free SQL Query Formatter (Prettifier) Tool - Tools
- Fetch as Google Crawl Error or Redirected Status - Google
- Spring Boot: Transactions Management with JDBCTemplate Example - Java
- How to know the Serial Number of MacBook on macOS Ventura 13.0 - MacOS
- apt-get list --installed packages in Ubuntu Linux - Ubuntu
- Quick way to install AWS CLI for macOS Terminal - AWS
- What is ValueError: math domain error and how to fix it - Python
- How to disable SharePoint subsite creation option for owners - SharePoint