In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today, the editor will share with you how Python uses the zipfile package module to compress the relevant knowledge points, the content is detailed, the logic is clear, I believe most people still know too much about this knowledge, so share this article for your reference, I hope you can get something after reading this article, let's take a look at it.
Brief introduction
ZIP file format is a commonly used archiving and compression standard. The zipfile module provides tools to create, read, write, add and list ZIP files.
This module cannot currently process split-volume ZIP files and supports decrypting encrypted files in ZIP archives, but it is not possible to create an encrypted file at this time. Decryption is very slow because it is implemented using native Python instead of C
Compressed file
Class zipfile.ZipFile (file, mode='r', compression=ZIP_STORED, allowZip64=True, compresslevel=None, *, strict_timestamps=True): ZipFile object, compression specifies the compression mode
ZipFile.write (filename, arcname=None, compress_type=None, compresslevel=None): write a compressed file. Filename is the original file name, arcname is the archive file name, and compress_type specifies the compression mode.
Compression mode means that ZIP_STORED does not compress Default value ZIP_DEFLATED commonly used ZIP compression ZIP_BZIP2BZIP2 compression ZIP_LZMALZMA compression import randomimport zipfilewith open ('1.txtcompression, mode='w') as f: for _ in range (1000): f.write (str (random.random ()) +') with zipfile.ZipFile ('1.zipcompression, mode='w', compression=zipfile.ZIP_DEFLATED) as zf: zf.write (' 1.txt') zf.write ('1.txtcompression,' 2.txt') Zipfile.ZIP_STORED) # original file name 1.txt Save as 2.txt, do not compress and extract files
ZipFile.namelist (): returns a list of files sorted by name
ZipFile.extract (member, path=None, pwd=None): extract the files to the specified directory
Import zipfilewith zipfile.ZipFile ('1.zip') as zf: for filename in zf.namelist (): zf.extract (filename,'.') Whether or not a ZIP file
Call zipfile.is_zipfile (filename)
Is a valid ZIP file that returns True, otherwise it returns False, and there is no return False at all
Import zipfilefor filename in ['1.txtreading,' 1.zipreading, '2.zip`]: print (filename, zipfile.is_zipfile (filename)) # 1.txt False # 1.zip True # 2.zip False read metadata
ZipFile.namelist (): returns a list of files sorted by name
ZipFile.infolist (): returns a list of ZipInfo objects
ZipFile.getinfo (name): returns a ZipInfo object
Import zipfilewith zipfile.ZipFile ('1.zipkeeper,' r') as zf: print (zf.namelist ()) # File list for info in zf.infolist (): print (info) print (info.filename) # File name print (info.date_time) # Modification time, available datetime.datetime (* info.date_time) print (info.compress_type) # Compression Type The values are zipfile.ZIP_STORED, ZIP_DEFLATED, ZIP_BZIP2, ZIP_LZMA print (info.comment) # comment print (info.extra) # extended field data print (info.create_system) # create the system used, 0 is Windows 3 create the PKZIP version used for Unix print (info.create_version) # print (info.extract_version) # extract the PKZIP version print (info.flag_bits) # flag bit print (info.volume) # header sub-volume number print (info.compress_size) # compressed data size print (info.file_size) # uncompressed data size print () compress files from other data sources
ZipFile.writestr (zinfo_or_arcname, data, compress_type=None, compresslevel=None): writes a file to a compressed file
Import randomimport zipfiledata = '.join ([str (random.random ()) +' 'for i in range (1000)]) with zipfile.ZipFile (' 1.zipkeeper, mode='w', compression=zipfile.ZIP_DEFLATED) as zf: zf.writestr ('1.txtjoin, data) writes to ZipInfo
Class zipfile.ZipInfo (filename='NoName', date_time= (1980, 1, 1, 0, 0, 0)): compressed file member information class
Import timeimport randomimport zipfiledata = '.join ([str (random.random ()) +' 'for i in range (1000)]) with zipfile.ZipFile (' 1.zipkeeper, mode='w', compression=zipfile.ZIP_DEFLATED) as zf: info = zipfile.ZipInfo ('1.txtjoin, date_time=time.localtime (time.time () info.compress_type = zipfile.ZIP_DEFLATED info.comment = Bachela comment' info.create_system = 0 zf.writestr (info, data) append file
Change the mode of ZipFile to append mode a
Import randomimport zipfilewith open ('2.txtlibraries, mode='w') as f: for _ in range (1000): f.write (str (random.random ()) +') with zipfile.ZipFile ('1.zipboxes, mode='a') as zf: zf.write (' 2.txt') create a ZIP containing the Python library
Class zipfile.PyZipFile (file, mode='r', compression=ZIP_STORED, allowZip64=True, optimize=-1): used to create ZIP classes that contain Python libraries
Zipfile_pyzipfile.py
Import sysimport zipfilewith zipfile.PyZipFile ('pyzipfile.zip', mode='w') as zf: zf.debug = 3 zf.writepy ('.') for name in zf.namelist (): print (name) sys.path.insert (0, 'pyzipfile.zip') import zipfile_pyzipfileprint (' Imported from:', zipfile_pyzipfile.__file__) these are all the contents of this article entitled "how Python uses the zipfile package module to compress". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.