The Python programming language is an object-oriented programming language with powerful functions. It is easy to use and grasp. Today, let's take a look at one of the most important application modules and the related application methods of the Python ZipFile module.
- Interpretation of Python compressed file basic application code example
- Summary of common functions of the Python OS Module
- How to find duplicate files in Python
- Main functions of four Python files
- Classic introduction to Python instance applications
The Python ZipFile module is used to compress and decompress zip code. Because it is a common zip format, this module is frequently used, here we will record how to use ZipFile. That is, it is convenient for you and others.
ZipFile has two very important classes: ZipFile and ZipInfo. In most cases, we only need to use these two classes. ZipFile is the main class used to create and read zip files, while ZipInfo is the information of each file stored in the zip file.
For example, to read a Python ZipFile module, assume that filename is the path of a file:
- Import ZipFile
- Z = ZipFile. ZipFile (filename, 'R ')
# Here, the second parameter is represented by r as reading the zip file, and w is creating a zip file.
- For f in z. namelist ():
- Print f
The above Code reads the names of all files in a zip package. Z. namelist () returns a list Of all file names in the compressed package.
Let's take a look at the following:
- import ZipFile
- z = ZipFile.ZipFile(filename, 'r')
- for i in z.infolist():
- print i.file_size, i.header_offset
Here, z. infolist () is used, and it returns information of all files in the compressed package, which is a ZipInfo list. A ZopInfo object contains information about a file in the compressed package. Commonly Used file names, file size, and header_offset are file names, file sizes, and file data offsets in the compressed package. In fact, the previous z. namelist () is the filename in the read ZopInfo to form a list to return.
The ZipFile read method is used to extract a file from the compressed package:
- import ZipFile
- z = ZipFile.ZipFile(filename, 'r')
- print z.read(z.namelist()[0])
In this way, the first file in z. namelist () is retrieved and output to the screen. Of course, it can also be stored to the file.
The following describes how to create a zip package:
The read method is similar to the following:
- Import ZipFile, OS
- Z = ZipFile. ZipFile (filename, 'w ')
# Note that the second parameter here is w, and the filename here is the name of the compressed package.
Suppose you want to add all the files named testdir to the compressed package. Here, only the files in the first-level subdirectory are added ):
- If OS. path. isdir (testdir ):
- For d in OS. listdir (testdir ):
- Z. write (testdir + OS. sep + d)
- # Close () is required!
- Z. close ()
The above code is very simple. Think about another problem. What if I want to add a test/111.txt file to the package and put it in test22/111.txt? In fact, this is the second parameter in the write method of the Python ZipFile module. You only need to call it like this:
- z.write("test/111.txt", "test22/111.txt")
The above is the knowledge about the Python ZipFile module.