[Python] Handling ZIP Files: How to Extract and Manipulate Using the zipfile Module

Python’s standard library zipfile module allows you to extract (unzip) ZIP archives or check the list of filenames inside without extracting.

This article explains operations ranging from basic handling to processing password-protected ZIP files, using a scenario of managing business data backup files.

目次

1. Checking ZIP File Contents (Without Extracting)

To check what files are inside the archive before extracting, use the namelist() method.

Source Code

import zipfile

# Target ZIP file
zip_target = "backup_data.zip"

print(f"--- Contents of {zip_target} ---")

# Open in 'r' mode (read)
with zipfile.ZipFile(zip_target, "r") as zf:
    # namelist() returns a list of filenames in the archive
    file_names = zf.namelist()
    
    for name in file_names:
        print(name)

Execution Result

--- Contents of backup_data.zip ---
data/
data/user_list.csv
data/sales_report.xlsx
readme.txt

2. Extracting All Files at Once

To extract all files within the ZIP to a specified folder, use the extractall() method.

Source Code

import zipfile

# Destination folder name
extract_dir = "restored_files"

with zipfile.ZipFile("backup_data.zip", "r") as zf:
    # extractall(destination_path)
    # If no path is specified, it extracts to the current directory
    zf.extractall(extract_dir)
    
print(f"Extracted all files to folder '{extract_dir}'.")

3. Extracting Specific Files Only

If you want to extract only a specific file (e.g., readme.txt), use the extract() method. Note: While you can pass a list to extractall, extract is simpler for single files.

Source Code

import zipfile

target_file = "readme.txt"
output_dir = "docs"

with zipfile.ZipFile("backup_data.zip", "r") as zf:
    # extract(filename, destination_path)
    path = zf.extract(target_file, output_dir)
    print(f"Extraction complete: {path}")

4. Extracting Password-Protected ZIP Files

If the file is password-protected, specify the password using the pwd argument in extractall() or extract(). Important: The password must be passed as bytes, not as a string (str).

Source Code

import zipfile

# Password-protected ZIP file
secure_zip = "confidential.zip"
# Password (add b"..." to make it bytes)
zip_password = b"SecretKey123"

try:
    with zipfile.ZipFile(secure_zip, "r") as zf:
        # Pass the password as bytes to the pwd argument
        zf.extractall("secret_data", pwd=zip_password)
        
    print("Successfully extracted encrypted ZIP.")

except RuntimeError as e:
    # RuntimeError is raised if the password is incorrect
    print(f"Extraction Error: {e}")
except zipfile.BadZipFile:
    print("The ZIP file is corrupted.")

Explanation

zipfile.ZipFile(file, mode)

This is the class used to open files. Specify mode='r' for reading. Using the with statement ensures the file is automatically closed after processing.

Path Separators

When specifying paths in Windows environments, it is safer to use raw strings like r"C:\path\to" or use forward slashes / (e.g., "C:/path/to") to prevent backslashes \ from being treated as escape characters. Python recognizes / as a path separator even on Windows.

The pwd Argument

Passing the password as a standard string (e.g., pwd="pass") will cause an error. You must pass it as bytes, either by using the b prefix (e.g., pwd=b"pass") or by converting it using .encode().

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次