Zipfile of python Module

Source: Internet
Author: User

Reprinted from: http://blog.csdn.net/zhaoweikid/archive/2007/05/30/1630842.aspx

This article was transferred from another of my blogs: http://hi.baidu.com/python23/

Zipfile is used to compress and decompress zip files in python. 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 zipfile, 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 (only the files in the first-level subdirectory are added here ):
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 ZipFile. Just call: z. write ("test/111.txt"," test22/111.txt ")

 

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.