Python ZipFile module details

Source: Internet
Author: User
Tags decompress zip zipinfo

The Python zipfile module is used to compress and decompress zip code. zipfile contains 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:
Copy codeThe Code is as follows:
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:
Copy codeThe Code is as follows:
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 ZipInfo object contains the information of a file in the compressed package. The commonly used files include filename, file_size, and header_offset, which 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 ZipInfo to form a list to return.
The ZipFile read method is used to extract a file from the compressed package:
Copy codeThe Code is as follows:
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, which is similar to the read method:
Copy codeThe Code is as follows:
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 (only the files in the first-level subdirectory are added here ):
Copy codeThe Code is as follows:
If OS. path. isdir (testdir ):
For d in OS. listdir (testdir ):
Z. write (testdir + OS. sep + d)
# Close () is required!
Z. close ()

The code above 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:
Copy codeThe Code is as follows:
Z. write ("test/111.txt"," test22/111.txt ")

The above is the knowledge about the Python ZipFile module.

Related Article

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.