Python realizes the use of batch compressed file and folder zipfile

  • 2021-11-29 07:31:47
  • OfStack

Directory "Python Compressed Folder" Import "zipfile" Module Import "python zip file" into "zipfile" module Supplement

zipfile is used in python to compress and decompress zip format coding. Because it is a very common zip format, this module is used frequently.

Here, make some records on the use of zipfile. That is, it is convenient for yourself and others.

Python zipfile module is used to compress and decompress zip format coding. To carry out related operations, it is necessary to instantiate an ZipFile object first. ZipFile accepts a string format compressed package name as its required parameter, and the second parameter is optional, indicating open mode, which is similar to file operation. There are three modes, r/w/a, which represent read, write and add respectively, and the default is r, that is, read mode.

There are two very important class in zipfile, ZipFile and ZipInfo. In most cases, we only need to use these two class. ZipFile is the main class used to create and read zip files while ZipInfo stores information for each of the zip files.

"Python Compressed Folder" Import "zipfile" Module


def zip_ya(startdir , file_news):
    startdir = ".\\123"  # Path to the folder to compress 
    file_news = startdir +'.zip' #  The name of the compressed folder 
    z = zipfile.ZipFile(file_news,'w',zipfile.ZIP_DEFLATED) # Parameter 1 : Folder name 
    for dirpath, dirnames, filenames in os.walk(startdir):
        fpath = dirpath.replace(startdir,'') # This 1 Sentence is very important, no replace If so, copy from the root directory 
        fpath = fpath and fpath + os.sep or ''# I am also depressed to understand this sentence. Realize the compression of the current folder and all the files contained 
        for filename in filenames:
            z.write(os.path.join(dirpath, filename),fpath+filename)
            print (' Compression succeeded ')
    z.close()

if__name__=="__main__"
    startdir = ".\\123"  # Path to the folder to compress 
    file_news = startdir +'.zip' #  The name of the compressed folder 
    zip_ya(startdir , file_news)

"python Compressed File" Import "zipfile" Module


import zipfile
def zip_files( files, zip_name ):
    zip = zipfile.ZipFile( zip_name, 'w', zipfile.ZIP_DEFLATED )
    for file in files:
        print ('compressing', file)
        zip.write( file )
    zip.close()
    print ('compressing finished')

files = ['.\\123.txt','.\\3.txt']# Location of files, multiple files are separated by "," 
zip_file = '.\\m66y.zip'# Compressed package name 
zip_files(files, zip_file)

Supplement

ZipFile also provides the following commonly used methods and properties:


ZipFile.getinfo(name)

Gets the information of the specified file in the zip document. Returns 1 zipfile. ZipInfo object, which includes the details of the file.


ZipFile.infolist()

Gets information about all the files in the zip document and returns a list of zipfile. ZipInfo.


ZipFile.namelist()

Gets a list of the names of all files in an zip document.


ZipFile.extract(member[, path[, pwd]])

Unzip the specified file in the zip document to the current directory. Parameter member specifies the name of the file to extract or the corresponding ZipInfo object; Parameter path specifies the folder where the parsed file is saved;


ZipFile.extractall([path[, members[, pwd]]])

Extract all files in the zip document to the current directory. The default value of the parameter members is a list of all file names in the zip document, or you can set it yourself and choose the file name to unzip.


ZipFile.printdir()

Print the information in the zip document to the console.


ZipFile.setpassword(pwd)

Set the password for the zip document.


ZipFile.read(name[, pwd])

Related articles: