Python Tutorial
Python Flow Control
Python Functions
Python Data Types
Python Date and Time
Python Files
Python String
Python List
Python Dictionary
Python Variable
Python Input/Output
Python Exceptions
Python Advanced
In this tutorial, you'll learn how to work with ZIP files in Python using the zipfile
module, which allows you to create, read, and extract ZIP files.
To create a new ZIP file, you can use the ZipFile
class from the zipfile
module. Here's an example of how to create a ZIP file containing one file:
import zipfile # Create a new ZIP file with zipfile.ZipFile('path/to/your/archive.zip', mode='w') as archive: # Add a file to the archive archive.write('path/to/your/file.txt', arcname='file.txt')
To add multiple files or directories, use a loop:
import zipfile import os files_to_zip = ['file1.txt', 'file2.txt', 'path/to/directory'] with zipfile.ZipFile('path/to/your/archive.zip', mode='w') as archive: for file_path in files_to_zip: arcname = os.path.basename(file_path) archive.write(file_path, arcname=arcname)
To read the contents of a ZIP file, you can use the ZipFile
class and its methods, such as namelist()
and getinfo()
:
import zipfile with zipfile.ZipFile('path/to/your/archive.zip', mode='r') as archive: # Get the list of files in the archive file_names = archive.namelist() # Print the list of files for name in file_names: print(name) # Get file info and print file size file_info = archive.getinfo(name) print(f"Size: {file_info.file_size} bytes")
To extract the contents of a ZIP file, you can use the extractall()
or extract()
methods of the ZipFile
class:
import zipfile with zipfile.ZipFile('path/to/your/archive.zip', mode='r') as archive: # Extract all files to the specified directory archive.extractall('path/to/your/destination')
To extract a single file from the ZIP archive:
import zipfile with zipfile.ZipFile('path/to/your/archive.zip', mode='r') as archive: # Extract a single file to the specified directory archive.extract('file.txt', 'path/to/your/destination')
By default, the ZipFile
class uses the ZIP_STORED
compression method, which means the files are stored without any compression. To compress files, you can use the ZIP_DEFLATED
method, which requires the zlib
library:
import zipfile import os files_to_zip = ['file1.txt', 'file2.txt'] compression_level = zipfile.ZIP_DEFLATED with zipfile.ZipFile('path/to/your/archive.zip', mode='w') as archive: for file_path in files_to_zip: arcname = os.path.basename(file_path) archive.write(file_path, arcname=arcname, compress_type=compression_level)
In this example, the files in the ZIP archive will be compressed using the ZIP_DEFLATED
method, which uses the Deflate algorithm for compression.
Create zip file in Python:
zipfile
module to create a zip file.import zipfile zip_file_path = 'example.zip' with zipfile.ZipFile(zip_file_path, 'w') as zip_file: zip_file.write('file1.txt') zip_file.write('file2.txt')
Zip and unzip files in Python:
zipfile
module.import zipfile # Zip files with zipfile.ZipFile('example.zip', 'w') as zip_file: zip_file.write('file1.txt') zip_file.write('file2.txt') # Unzip files with zipfile.ZipFile('example.zip', 'r') as zip_file: zip_file.extractall('extracted_files')
Compress and decompress files in Python:
gzip
module.import gzip import shutil # Compress file with open('file.txt', 'rb') as file: with gzip.open('file.txt.gz', 'wb') as gzip_file: shutil.copyfileobj(file, gzip_file) # Decompress file with gzip.open('file.txt.gz', 'rb') as gzip_file: with open('decompressed_file.txt', 'wb') as file: shutil.copyfileobj(gzip_file, file)
Zip a directory in Python:
shutil.make_archive
to zip a directory.import shutil shutil.make_archive('example_directory', 'zip', 'directory_to_zip')
Python shutil.make_archive()
example:
shutil.make_archive
for zipping and archiving.import shutil shutil.make_archive('archive', 'zip', 'source_directory')
Extract files from a zip archive in Python:
zipfile
to extract files from a zip archive.import zipfile with zipfile.ZipFile('example.zip', 'r') as zip_file: zip_file.extract('file1.txt', 'destination_folder')
Python zipfile.extractall()
method:
extractall()
to extract all files from a zip archive.import zipfile with zipfile.ZipFile('example.zip', 'r') as zip_file: zip_file.extractall('destination_folder')
Add files to an existing zip archive in Python:
zipfile
.import zipfile with zipfile.ZipFile('existing_archive.zip', 'a') as zip_file: zip_file.write('new_file.txt')
Python zip directory and subdirectories:
shutil.make_archive
.import shutil shutil.make_archive('example_directory', 'zip', 'directory_to_zip')
Recursive zip and unzip in Python:
import shutil # Recursive zip shutil.make_archive('example_directory', 'zip', 'directory_to_zip') # Recursive unzip shutil.unpack_archive('example_directory.zip', 'extracted_directory')
Password-protected zip files in Python:
zipfile
.import zipfile with zipfile.ZipFile('password_protected.zip', 'w', zipfile.ZIP_DEFLATED) as zip_file: zip_file.setpassword(b'my_password') zip_file.write('file1.txt') zip_file.write('file2.txt')
Working with zip file comments in Python:
import zipfile # Add comment to a zip file with zipfile.ZipFile('example.zip', 'w') as zip_file: zip_file.comment = b'This is a comment' zip_file.write('file1.txt') # Read comment from a zip file with zipfile.ZipFile('example.zip', 'r') as zip_file: comment = zip_file.comment print(f'Comment: {comment.decode("utf-8")}')
Create a self-extracting zip file in Python:
shutil.make_archive
.import shutil shutil.make_archive('self_extracting', 'zip', 'source_directory')
Splitting and joining large zip files in Python:
import shutil # Split large zip file shutil.split('large_file.zip', 'split_zip', 1000000) # Join split zip files shutil.join('split_zip', 'large_file_reconstructed.zip')
Check if a file is a zip file in Python:
zipfile
.import zipfile is_zip = zipfile.is_zipfile('example.zip') print(f'Is a zip file: {is_zip}')
Extract specific files from a zip archive in Python:
import zipfile with zipfile.ZipFile('example.zip', 'r') as zip_file: files_to_extract = ['file1.txt', 'file2.txt'] for file in files_to_extract: zip_file.extract(file, 'destination_folder')
Error handling when working with zip files in Python:
import zipfile try: # Try to create a zip file with zipfile.ZipFile('example.zip', 'w') as zip_file: zip_file.write('file1.txt') zip_file.write('file2.txt') except Exception as e: print(f'Error: {e}')