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 Python unzips files

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces how to extract Python files, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

1 Compression 1.1 create zipfile object zipfile.ZipFile (file, mode='r', compression=0, allowZip64=True, compresslevel=None)

Create a Zipfile object. Main parameters:

1 > file package name

2 > mode: read'r'or write'w 'mode

3 > compression: set compression format

4 > compresslevel: compression level

Compression format classification:

Format description corresponding to compresslevelZIP_STORED do not compress invalid ZIP_DEFLATED requires zlib support 0~9ZIP_BZIP2 requires bz2 support 0~9ZIP_LZMA requires lzma support invalid 1.2 add compressed file zipobj.write (self, filename, arcname=None, compress_type=None, compresslevel=None)

Parameter description:

1 > fiename: add file path

2 > arcname: path and name of archive file

1.3 add compressed data zipobj.writestr (zinfo_or_arcname, data, compress_type=None, compresslevel=None)

Parameter description:

1 > zinfo_or_arcname: archive file name

2 > data: compress data str or byte. If the data is str, you need to encode Utf-8 first.

1.4 close

Close the compressed file:

An example of zipobj.close () 1.5:

The compressed directory is as follows:

Code implementation:

Import zipfilelogdir ='/ home/linux/logs'zipname ='/ home/linux/logs.zip'# create the zip object, fzip = zipfile.ZipFile (zipname, 'walled, zipfile.ZIP_DEFLATED) # traverse the directory to be compressed flist = os.listdir (logdir) for name in flist: fpath = os.path.join (logdir, name) # write the file to be compressed fzip.write (fpath) # close fzip.close ()

Enter the directory to decompress, and the file path is as follows:

As you can see, we add the whole directory to the compressed file, but what we prefer to do is logs/*.log.

Make changes to the code:

Logdir ='/ home/linux/logs'zipname ='/ home/linux/logs.zip'import zipfile# create the zip object, fzip = zipfile.ZipFile (zipname, 'walled, zipfile.ZIP_DEFLATED) # traverse the directory to be compressed flist = os.listdir (logdir) # get the compressed directory name basename = os.path.basename (logdir) for name in flist: fpath = os.path.join (logdir, name) arcname = os.path.join (basename, name) # write to the file to be compressed And add the archive file name fzip.write (fpath, arcname=arcname) # close fzip.close ()

After compression, extract the content:

This is what we want.

two。 Get compressed package information

Open the package:

Frzip = zipfile.ZipFile (zipname, 'ringing, zipfile.ZIP_DEFLATED): open read-only

Frzip.printdir (), which displays the compressed package file information

File Name Modified Sizelogs/1003.log 2019-04-09 10:00:10 17logs/1001.log 2019-04-09 10:00:06 17logs/1004.log 2019-04-09 10:00:12 17logs/1002.log 2019-04-09 10:00:08 17logs/1000.log 2019-04-09 10:00:04 17

Frzip.filelist: record the information of the compressed package file, which is similar to printdir and contains the following:

[,]

Frzip.getinfo (name): get the information of the specified arcname:

Frzip.getinfo ('logs/1003.log') # output result:

Frzip.namelist (): get a list of compressed files

['logs/1003.log',' logs/1001.log', 'logs/1004.log',' logs/1002.log', 'logs/1000.log'] 3 unzip the package

Frzip.extract (member, path=None, pwd=None): extract a file to a specified directory

Parameters:

Member: files in a compressed package

Path: extract to the specified directory, and extract to the current directory by default

Frzip.extractall (path=None, members=None, pwd=None): extract multiple files to a specified directory

Parameters:

Path: the specified decompression directory

Members: specify to extract files. Extract all files by default.

Extract the specified file to the specified directory:

# you can define the path zipname ='/ home/linux/logs.zip'extractpath ='/ home/linux/1'#. Choose the compression format: frzip = zipfile.ZipFile (zipname, 'ringing, zipfile.ZIP_DEFLATED) extractfile = frzip.namelist () if len (extractfile): extname = extractfile [0] print (' extractfile:', extname) frzip.extract (extractfile [0], extractpath) frzip.close ()

Output result: extract file: logs/1003.log, log file in the extractpath directory

Use frzip.extractall to extract all files to the specified directory:

# you can define the path zipname ='/ home/linux/logs.zip'extractpath ='/ home/linux/1'# note the compression format: frzip = zipfile.ZipFile (zipname, 'rushing, zipfile.ZIP_DEFLATED) # compress all files to the specified directory frzip.extractall (extractpath) frzip.close ()

There are other modules besides zipfile:

Import gzip

Import tarfile

These modules are used in a similar way, which we will explain in detail in later articles.

Thank you for reading this article carefully. I hope the article "how to extract the Python File" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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