Zipfile In the Python Module

Source: Internet
Author: User
Tags zipinfo

Python has the most abundant and powerful class libraries in the scripting language and is sufficient to support most daily applications. Python modules and C can work together and can be embedded into C or C ++ applications, therefore, the Python language can be used to provide script interfaces for applications.

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. The Python module ZipFile is the main class. It is used to create and read zip files, while ZipInfo stores the information of each file in the zip file.

For example, to read a zipfile, assume that filename is the path of a file:

 
 
  1. Import zipfile
  2.  
  3. Z=Zipfile. ZipFile (filename, 'R') # Here, the second parameter is represented by r to read the zip file, and w is to create a zip file.
  4.  
  5. For f in z. namelist ():
  6. Print f
  7. Import zipfile
  8.  
  9. Z=Zipfile. ZipFile (filename, 'R ')
  10.  
  11. For I in z. infolist ():
  12. Print I. file_size, I. header_offset

The Python module z. infolist () is used here. It returns the information of all the files in the compressed package, which is a ZipInfo list. A ZopInfo object contains information about a file in the compressed package. The commonly used file names are filename, file_size, and header_offset, respectively.

File size, which is the offset of file data 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:

The 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?

  1. How to embed Python into C ++ applications?
  2. In-depth discussion of Ruby and Python syntax comparison
  3. Introduction to Python
  4. Python Learning Experience: version, IDE selection and coding Solutions
  5. Analysis of Python GIL and thread security

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.