Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize batch Compression of Files by Python

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "Python how to achieve batch compression of files", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "Python how to achieve batch compression of files" bar!

Catalogue

[Python compressed folder] Import "zipfile" module

[python compressed file] Import "zipfile" module

Supplement

Zipfile is used for compression and decompression in python for zip format coding. Because it is a very common zip format, this module is also used frequently.

Make some notes on how to use zipfile here. It is convenient for yourself as well as for others.

The Python zipfile module is used to do compression and decompression in zip format. To perform related operations, you first need to instantiate a ZipFile object. ZipFile accepts a string format compressed package name as its required parameter, the second parameter is optional, indicating the open mode, similar to file operation, there are three modes of r/w/a, which represent read, write and add, respectively. The default is r, namely read mode.

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

[Python compressed folder] Import "zipfile" module

Def zip_ya (startdir,file_news): startdir = ".\\ 123" # folder path to be compressed file_news = startdir + '.zip' # compressed folder name 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 is very important If you don't replace, I'm a little depressed to copy fpath = fpath and fpath + os.sep or''# from the root directory. Implement the compressed for filename in filenames: z.write (os.path.join (dirpath, filename) of the current folder and all the files it contains Fpath+filename) print ('compressed successfully') z.close () if__name__== "_ main__" startdir = ".\\ 123" # folder path to be compressed file_news = startdir + '.zip' # name of the compressed folder zip_ya (startdir File_news) [python zip file] Import "zipfile" module

Import zipfiledef zip_files (files, zip_name): zip = zipfile.ZipFile (zip_name, 'txt, zipfile.ZIP_DEFLATED) for file in files: print (' compressing', file) zip.write (file) zip.close () print ('compressing finished') files = ['.\ 123.txtpointer.\ 3.txt'] # file location, multiple files use " "separate zip_file ='.\\ m66y.zip'# package name zip_files (files, zip_file) add

ZipFile also provides the following common methods and properties:

ZipFile.getinfo (name)

Gets information about the specified file within the zip document. Returns a zipfile.ZipInfo object that includes the details of the file.

ZipFile.infolist ()

Gets the 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 the zip document.

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

Extract the specified files in the zip document to the current directory. The parameter member specifies the name of the file to be extracted or the corresponding ZipInfo object, and the 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 to select the file name to extract.

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]) Thank you for your reading, the above is the content of "Python how to achieve batch compression of files". After the study of this article, I believe you have a deeper understanding of how Python achieves batch compression of files, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report