Home > Article > Backend Development > How to use the zipfile module to create and decompress ZIP files in Python 2.x
How to use the zipfile module in Python 2.x to create and decompress ZIP files
Introduction:
ZIP file is a common archive file format that is often used to compress and package files and folders. Python provides the zipfile module to create and decompress ZIP files. This article will introduce how to use the zipfile module to create and decompress ZIP files in Python 2.x.
Installation:
Python 2.x already includes the zipfile module by default, no additional installation is required.
Create ZIP file:
The following is a sample code that shows how to use the zipfile module to create a ZIP file that contains specified files and folders.
import zipfile import os def create_zip_file(zip_file_name, file_list): with zipfile.ZipFile(zip_file_name, 'w') as zip_file: for file_path in file_list: zip_file.write(file_path, os.path.basename(file_path)) # 示例使用 files = ['file1.txt', 'file2.txt', 'folder'] # 要包含在 ZIP 文件中的文件列表 create_zip_file('my_archive.zip', files)
In the above code, we first imported the zipfile and os modules. Then, a create_zip_file function is defined that accepts a ZIP file name and a list of files as parameters. Internally, the function uses the ZipFile class of the zipfile module to create a ZIP file instance, then loops through the file list, and uses the write method of the ZipFile object to add the file to the ZIP file, and uses the basename method of the os module to obtain the base file name of the file. . Finally, at the end of the with block, the ZIP file will be closed automatically.
Extracting ZIP files:
The following is a sample code showing how to use the zipfile module to decompress a ZIP file.
import zipfile def extract_zip_file(zip_file_name, extract_folder): with zipfile.ZipFile(zip_file_name, 'r') as zip_file: zip_file.extractall(extract_folder) # 示例使用 zip_file = 'my_archive.zip' # 要解压的 ZIP 文件名称 extract_folder = 'extracted' # 解压后的文件夹名称 extract_zip_file(zip_file, extract_folder)
In the above code, we define an extract_zip_file function, which accepts a ZIP file name and an unzipped folder name as parameters. Internally, the function uses the ZipFile class of the zipfile module to create a ZIP file instance, and extracts the ZIP file to the specified folder through the extractall method of the ZipFile object. Likewise, at the end of the with block, the ZIP file will be automatically closed.
Summary:
By using the zipfile module, we can easily create and decompress ZIP files in Python 2.x. This article describes how to use the zipfile module to create and decompress ZIP files, and provides corresponding code examples. These sample codes can be easily used in practical applications. I hope they will be helpful to everyone.
The above is the detailed content of How to use the zipfile module to create and decompress ZIP files in Python 2.x. For more information, please follow other related articles on the PHP Chinese website!