Features of the Python ZipFile Module

Source: Internet
Author: User
Tags decompress zip zipinfo

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:

 
 
  1. Import ZipFile
  2. 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.
  3. For f in z. namelist ():
  4. 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:

 
 
  1. import ZipFile  
  2. z = ZipFile.ZipFile(filename, 'r')  
  3. for i in z.infolist():  
  4. 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:

 
 
  1. import ZipFile  
  2. z = ZipFile.ZipFile(filename, 'r')  
  3. 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:

 
 
  1. Import ZipFile, OS
  2. 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 ):

 
 
  1. If OS. path. isdir (testdir ):
  2. For d in OS. listdir (testdir ):
  3. Z. write (testdir + OS. sep + d)
  4. # Close () is required!
  5. 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:

 
 
  1. 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.