The Zipfile__python of the Python standard library

Source: Internet
Author: User
Python standard library ZipFile

What is a zip file? Why zip-packaged files on the network are more common. Rather than RAR?

Zip is a compressed archive of files, zip open source.

Python's ZipFile module, with two major classes
ZipFile ()

function to determine if it is a zip type file

ZipFile. Is_zipfile (filename)

Returns True If filename is a valid ZIP file based to its magic number,otherwise Returns False. FileName May is a file or File-like object too
Read the contents of a file directly from a compressed file
ZipFile. Read (name [, PWD]) # actually called Self.open (). Read ()

Return the bytes of the file name in the archive. The name is the name of Thefile in the archive
ZipFile. Open (name [, mode [, PWD]])

Extract a member from the archive as a File-like object (zipextfile)
ZipFile. Close ()

Close the archive file. You are must call Close () before exiting your Programor essential the won't be records. Zipinfo ()

# compress to specified file
def file_zip (SRC, DST):
    zip_obj = ZipFile (DST, mode= ' W ')
    zip_obj.write (src)
    zip_obj.close ( )


 # decompression
def unzip (SRC):
    zip_obj = ZipFile (src, mode= ' r ') for
    info in zip_obj.filelist:
        zip_ Obj.extract (Member=info)
    zip_obj.close ()


ZipFile command-line interface

Usage:
    zipfile.py-l zipfile.zip        # Show listing of a zipfile zipfile.py-t        # Test if a zip The file is valid
    zipfile.py-e zipfile.zip target # Extract ZipFile to target dir
    zipfile.py-c zipfile.zip src. .. # Create ZipFile from sources

-C Create

-e Extract (extract)

-L List


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.