Python Unzip Utility Guide
A complete standalone Python script to extract .zip files — no dependencies required.
View the ScriptRequirements & Setup
System Requirements
- Python 3.x — the script uses only built-in modules.
- No pip packages needed.
How to Save the Script
- Open a text editor (VS Code, Notepad, Sublime Text).
- Copy the code block above into a new file.
- Save as
unzip.pyin your working directory.
How to Run the Script
Open your terminal or command prompt and navigate to the directory where unzip.py is saved.
>
python unzip.py example.zip
Extracts into a new folder named after the zip file in the current directory.
>
python unzip.py example.zip /path/to/target_directory
Extracts the contents directly into a specified target directory.
Optional: Build an Executable
If you want to compile unzip.py into a standalone executable so users don't need Python installed:
1
Install PyInstaller:
pip install pyinstaller
2
Build the executable:
pyinstaller --onefile unzip.py
3
Find your built executable inside the generated dist/ folder.
The Code (unzip.py)
Copy and save this into a file named unzip.py.
import zipfile
import sys
import os
def extract_zip(zip_path, extract_to=None):
""
Extracts a zip file to the specified destination directory.
If no destination is provided, it extracts into a folder named after the zip file.
"
if not os.os.path.exists(zip_path):
print(f"Error: The file '{zip_path}' does not exist.")
return
# If destination is not specified, use a folder named after the zip file
if extract_to is None:
extract_to = os.os.splitext(zip_path)[0]
# Create destination directory if it doesn't exist
os.(extract_to, exist_ok=True)
try:
with zipfile.ZipFile(zip_path, ) as zip_ref:
zip_ref.extractall(extract_to)
print(f"Successfully extracted '{zip_path}' to '{extract_to}'.")
except zipfile.BadZipFile:
print(f"Error: '{zip_path}' is not a valid zip file or is corrupted.")
except Exception as e:
print(f"An unexpected error occurred: {e}")
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: python unzip.py <path_to_zip_file> [destination_folder]")
sys.exit(1)
zip_file_path = sys.argv[1]
destination_folder = sys.argv[2] if len(sys.argv) > 2 else None
extract_zip(zip_file_path, destination_folder)