Python ZipFile Module Detailed _python

Source: Internet
Author: User
Tags zipinfo
The Python zipfile module is used to compress and decompress the ZIP format code, ZipFile has two very important classes, ZipFile and Zipinfo, in the vast majority of cases, we just need to use the two class. ZipFile is the primary class that is used to create and read zip files, and Zipinfo is the information for each file of the stored zip file.
For example, to read a Python ZipFile module, here assume that filename is the path to a file:
Copy Code code as follows:

Import ZipFile
Z =zipfile. ZipFile (filename, ' R ')
# The second parameter here uses R to read the zip file, W is to create a zip file
For f in Z.namelist ():
Print F

The code above reads the names of all the files in a zip-zipped package. Z.namelist () Returns a list of all the file names in the compressed package.
And look at the following one:
Copy Code code as follows:

Import ZipFile
z = zipfile. ZipFile (filename, ' R ')
For I in Z.infolist ():
Print I.file_size, I.header_offset

This uses z.infolist (), which returns information about all the files in the package, which is a list of zipinfo. A Zipinfo object contains information about a file in a compressed package, which is commonly used for filenames, file_size, header_offset, filename, file size, and migration of file data in a compressed package. In fact, the previous z.namelist () is read in the Zipinfo filename, composed of a list returned.
The way to extract a file from a compressed package is to use the ZipFile Read method:
Copy Code code as follows:

Import ZipFile
z = zipfile. ZipFile (filename, ' R ')
Print Z.read (z.namelist () [0])

This reads the first file in the Z.namelist () and outputs it to the screen, and of course it can be stored in a file. Here's how to create a ZIP compression package, which is similar to the way it reads:
Copy Code code as follows:

Import ZipFile, OS
z = zipfile. ZipFile (filename, ' W ')
# Note that the second argument here is W, where filename is the name of the compressed package

Suppose you want to add a file called TestDir to a compressed package (add only the files in the first level subdirectory):
Copy Code code as follows:

If Os.path.isdir (TestDir):
For D in Os.listdir (TestDir):
Z.write (Testdir+os.sep+d)
# Close () must be called!
Z.close ()

The code for the face is very simple. Think of another problem, if I add a test/111.txt to the zip package, what do I want to do with the test22/111.txt in the bag? This is actually the second parameter in the Write method of the Python ZipFile module. You only need to call this:
Copy Code code as follows:

Z.write ("Test/111.txt", "Test22/111.txt")

The above is the knowledge about Python ZipFile modules that we have introduced to you.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.