How to use the zipfile module to create and decompress ZIP files in Python 3.x
Introduction:
In Python programming, handling file compression and decompression is a common task. The zipfile module provides a convenient way to create and decompress ZIP files. This article will introduce how to use the zipfile module to create and decompress ZIP files and provide corresponding code examples.
1. Create a ZIP file:
To create a ZIP file, we need to import the zipfile module first, and then use the ZipFile object to create and write the file. Here is a simple example code:
import zipfile # 创建一个名为example.zip的ZIP文件 with zipfile.ZipFile('example.zip', 'w') as myzip: # 向ZIP文件中写入一个名为file1.txt的文件 myzip.write('file1.txt') # 向ZIP文件中写入一个名为file2.txt的文件 myzip.write('file2.txt') # 可以通过使用相对路径来写入文件到ZIP文件的指定目录下 myzip.write('folder/file3.txt', 'data/file3.txt')
In the above code, we have used Python's context manager (with statement) to ensure that the ZIP file is closed after completing the operation. Inside the with
statement, we write the file to the ZIP file through the write()
method. write()
The method contains two parameters. The first parameter is the file name or path to be written, and the second parameter is the path of the file in the ZIP file. If the second argument is not provided, the file will be written to the root directory of the ZIP file.
2. Extract ZIP files:
To decompress a ZIP file, we can use the extractall()
method of the zipfile.ZipFile object. Here is a sample code to decompress a ZIP file:
import zipfile # 打开名为example.zip的ZIP文件 with zipfile.ZipFile('example.zip', 'r') as myzip: # 解压ZIP文件的所有内容到当前目录下 myzip.extractall()
In the above code, we use a context manager similar to the one used to create the ZIP file to ensure that the ZIP file is closed after the decompression operation is completed. We use the extractall()
method to extract all the contents of the ZIP file to the current directory. If you need to extract the ZIP file to a specified directory, you can pass a parameter in the extractall()
method to specify the decompression target path.
3. Traverse the contents of the ZIP file:
In addition to creating and decompressing the ZIP file, we can also traverse all files and directories in the ZIP file. The zipfile.ZipFile object provides the namelist()
method to obtain a list of all files and directories in the ZIP file. The following is a sample code that traverses the contents of a ZIP file:
import zipfile # 打开名为example.zip的ZIP文件 with zipfile.ZipFile('example.zip', 'r') as myzip: # 获取ZIP文件中的所有文件和目录列表 filelist = myzip.namelist() # 遍历文件列表并打印 for filename in filelist: print(filename)
In the above code, we use the namelist()
method to obtain the file list of the ZIP file and assign it to a variable filelist
. We then use for
to loop through filelist
and print the file names.
Conclusion:
Using the zipfile module, we can easily create and decompress ZIP files. This article introduces how to use the zipfile module to create ZIP files, decompress ZIP files, and traverse the contents of ZIP files, and provides corresponding code examples. I hope this article has been helpful for you in handling file compression and decompression tasks in Python programming.
The above is the detailed content of How to use the zipfile module to create and decompress ZIP files in Python 3.x. For more information, please follow other related articles on the PHP Chinese website!