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: Convert Date to DateTime
- How to sort a List using Lambda in Python
- Python matplotlib segmentation fault: 11 macOS Big Sur
- What is Terminal Velocity and its Formula? How to calculate it programmatically?
- How to install Python 3.11 on Mac
- How to flatten a nested list in Python
- Python: Pandas Merge DataFrames on Index Example
- How to Run all Cells at Once Jupyter Notebook
- Python - Convert float to String
- How to add borders to tkinter label text
- How to Exit a Loop in Python Code
- [Python] Fix: ValueError: All arrays must be of the same length
- Sorting an array using Bubble Sort in Python Programming
- How to Unzip a file using Python
- Python: Merge DataFrames Pandas Outer Join Example
- Change label (text) color in tkinter
- Convert Float to String in Python
- Fix: fatal error: No such file or directory compilation terminated
- Python: Access index/counter of a for loop iteration
- Import Other Python Files Examples
- How to install Anaconda on Mac (M1/M2 Mac)
- Python Regular Expression to Find All Matches in List
- How to Read a binary File with Python
- How to disable warnings while Python file execution
- Know current Python Version
More Posts:
- Meaning of [::-1] in Python Slicing - Python
- 'Edit Document' Requires a Windows Sharepoint Services-compatible application and Microsoft Internet Explorer 6.0 or higher - SharePoint
- java: ']' expected error [fixed] - Java
- How to show console in Eclipse IDE - Eclipse
- How to add Widgets to MacBook Desktop - HowTos
- Download Google Chrome setup exe file using PowerShell - Powershell
- Set Custom Background Wallpaper on Mac Terminal (macOS Ventura) - MacOS
- Java JDBC with Join Queries Example - Java