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:
- What is Microsoft 365 Message You are using more licenses on your trial than what you will purchase once the free trial ends. - Microsoft
- Mac: Sign in Required We cant upload or download your changes because your cached credentials have expired [Word Excel] - Windows
- Java Jackson ObjectMapper Class with Examples - Java
- Calculate Area of Trapezoid - C-Program
- How to open CMD for current file/folder location in Notepad++ - NotepadPlusPlus
- How to Change or Set a new Password for your Wi-Fi Router - HowTos
- Create simple struts2 project using maven commands - Java
- How to always show Bookmarks bar on Google Chrome Browser - Chrome