Explain Codes LogoExplain Codes Logo

Unzipping files in Python

python
file-paths
zipfile
pathlib
Alex KataevbyAlex Kataev·Mar 9, 2025
TLDR

Extract ZIP files swiftly with Python's in-built zipfile module. Here's the quick code you need:

import zipfile with zipfile.ZipFile('your_file.zip') as z: z.extractall('destination_folder')

Just swap 'your_file.zip' with your zip file's path and 'destination_folder' with your desired path.

Dealing with file paths in Python

When it comes to a proper unzipping, how you handle file paths is critical. Let's decode this a bit according to different Python versions:

Python 3.2 and above:

Context management ensures resources are handled correctly. This is how you'll want to code:

from zipfile import ZipFile with ZipFile('your_archive.zip') as archive: archive.extractall('extracted') # Door closed after unpacking. No resource leaks.

Python 3.7 and above with pathlib:

from zipfile import ZipFile from pathlib import Path with ZipFile(Path('your_archive.zip')) as archive: archive.extractall(Path('extracted')) # The Path helps us in not getting lost in directories.

In case of older Python versions:

from zipfile import ZipFile archive = ZipFile('your_archive.zip') archive.extractall('extracted') archive.close() # Never forget to close. We're not raised in a barn!

Extracting selective files

In case you want to extract specific files, here's your handy code:

with zipfile.ZipFile('your_file.zip') as z: z.extract('specific_file.txt', 'destination_folder') # Like finding that sock you thought the dryer ate.

And if you have a list of specific files:

files_to_extract = ['first_file.txt', 'second_file.jpg', 'third_file.doc'] with zipfile.ZipFile('your_file.zip') as z: for file in files_to_extract: z.extract(file, 'destination_folder') # Multi-tasking like a boss!

Working with shutil for different archive formats

The shutil module plays nice with various archive formats. Here's the trick:

import shutil shutil.unpack_archive('your_archive.zip', 'destination_folder') # A method smarter than a 5th grader, detects archive format.

Practical guide to common issues

Unzipping can sometimes unzip some unexpected issues. Let's combat them:

Password-protected ZIP files:

Encrypted ZIPs? No problem. Use setpassword():

with zipfile.ZipFile('protected.zip') as z: z.setpassword(b'yourpassword') z.extractall('destination_folder') # Remember your password, or you'll have a ZIP of a time!

Large ZIP files:

For large files, beware of memory constraints:

with zipfile.ZipFile('large_file.zip') as z: for file_info in z.infolist(): with z.open(file_info) as file: process_file(file) # One file at a time, just like eating an elephant.

Corrupted ZIP files:

Suspect corruption? Catch exceptions:

try: with zipfile.ZipFile('corrupted.zip') as z: z.extractall('destination_folder') except zipfile.BadZipFile as e: handle_error(e) # Catch it before it falls!

Ensuring compatibility across Python versions

Making your code universal requires a little bit extra. Here's what you need to remember when dealing with zip files:

  • Use try-except blocks for bulletproof error handling.
  • Verify the Python version supports specific zipfile features before using.
  • If possible, use pathlib for clean and intuitive file path management.