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!
Author Info:
Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.
Follow him on: X
You can also reach out to him via e-mail: rakesh@code2care.org
More Posts related to Python,
- How to convert int to ASCII in Python
- How to make use of SQLite Module in Python?
- Split a String into Sub-string and Parse in Python
- Python: Pandas Rename Columns with List Example
- How to run Python file from Mac Terminal
- How to Exit a Loop in Python Code
- Python: How to Plot a Histogram using Matplotlib and data as list
- MD5 Hashing in Python
- Jupyter: Safari Cant Connect to the Server localhost:8888/tree
- Fix: AttributeError: str object has no attribute decode. Did you mean: encode?[Python]
- How to Read a binary File with Python
- How to add two float numbers in Python
- Python: How to install YAML Package
- Python: How to Save Image from URL
- What is Markdown in Jupyter Notebook with Examples
- How to change the Python Default version
- 33: Python Program to send an email vid GMail
- How to comment code in Python
- How to Fix AttributeError in Python
- Fix: error: Jupyter command `jupyter-nbconvert` not found [VSCode]
- How to comment out a block of code in Python
- List of All 35 Reserved Keywords in Python Programming Language 3.11
- Import Other Python Files Examples
- Python: How to add Progress Bar in Console with Examples
- 3 Ways to convert bytes to String in Python
More Posts:
- [fix] openssl No such file or directory error C++ - Ubuntu
- [Solution] macOS could not be installed on your computer OSInstall.mpkg appears to be missing or damaged - MacOS
- macOS: Change Weather App Temperature unit from Fahrenheit to Degree - MacOS
- Adding Sub Headings to Bootstrap Header tags - Html
- Facebook Graph API Unavailable - Facebook
- How to run bash command in background - Bash
- Installing Android Studio Dolphin on Mac with Apple (M1/M2) Chip - Android-Studio
- How to Disable EditText Keyboard Android App - Android