Python | compression and decompression of files / folders and information reading

This article describes how to compress single or multiple files, or all files in a folder; How to decompress a single file or all files in a compressed file; How to read compressed file information.

Feasible Library

Several viable third-party libraries for compressing files / folders or decompressing

Library nameexplain
zipfile
tarfileThe usage of tarfile is similar to that of zipfile. For details, see here
shutilSee for details Official documents Note that shutil make_ Archive is not thread safe.

Read information

zipfile

  • Read the zip file under the target path and zip it_ File to store
    fromPath = './TEST/output.zip'
    zip_file = zipfile.ZipFile(fromPath)
  • Compress all files in the file
    fileList = zip_file.namelist()
  • Size of a file before and after compression
    A file is file, file = fileList[2]
    ——The file size before compression is b_size = zip_file.getinfo(file).file_size
    ——The compressed file size is a_size = zip_file.getinfo(file).compress_size

compress

Here is a summary of how to use the third-party library mentioned above to compress and package the corresponding files / folders.

zipfile

Here is a summary of how to use zipfile to compress single or multiple files, all files in a specified folder, or add files or entire folders to a specified zip file

Specify a single file or multiple files

import zipfile
import os

# 1. define target folder and target path to export zip file
fromPath1 = './test.py'
fromPath2 = './DataAnalysis/Pandas/RawData.xlsx'
toPath = './Test/output.zip'

# 2. check if target path to export output.zip file exists: if yes, pass; if not, pass;
toFolder, toFilename = os.path.split(toPath)
if not os.path.exists(toFolder):
    os.mkdir(path=toFolder)
toFolder, toFilename = os.path.split(toPath)

# 3.
with zipfile.ZipFile(toPath, 'w') as Export:
    Export.write(fromPath1)
    Export.write(fromPath2)

Define the path where the compressed and packaged target file is located, such as fromPath1 and fromPath2 here

Determine whether the target path toFolder exists:
If yes, proceed to the next step; If it does not exist, create toFolder.

Write the files corresponding to fromPath1 and fromPath2 to the output of toPath with Export zip


All files in the specified folder

# 1. define target folder and target path to export zip file
fromPath = './DataAnalysis/Pandas'                          
toPath = './Test/output.zip'              

# 2. check if target path to export output.zip file exists: if yes, pass; if not, pass;
toFolder, toFilename = os.path.split(toPath)
if not os.path.exists(toFolder):
    os.mkdir(path=toFolder)

# 3. check if target path is for a file or a folder:
with zipfile.ZipFile(toPath, 'w') as Export:
    # 3.1 if file: write file in target path
    if os.path.isfile(fromPath):
        Export.write(filename=fromPath)
    # 3.2 if folder: write files under folder into target path
    if os.path.isdir(fromPath):
        for root, dirs, files in os.walk(top=fromPath):
            for singleFile in files:
                if singleFile != toFilename:
                    filepath = os.path.join(root, singleFile)
                    Export.write(filename=filepath)

Define the target folder to be compressed and packaged, and the target file name and path of the output compressed zip file.

Determine whether the target path toFolder exists:
If yes, proceed to the next step; If it does not exist, create toFolder.

Judge that the target path fromPath is the path where the file or folder is located:
If it is a file, it is written as shown in specifying a single file or multiple files
If it is a folder, poll the folder path and write its subordinate files


Add file or folder

import zipfile
import os

fromPath = './test.py'
toPath = './Test/output.zip'

with zipfile.ZipFile(toPath, 'a') as Add:
    if os.path.isfile(fromPath):
        Add.write(fromPath)
    if os.path.isdir(fromPath):
        for root, dirs, files in os.walk(fromPath):
            for singleFile in files:
                filepath = os.path.join(root, singleFile)
                Add.write(filepath)

Define the path of the file to be added and the path of the compressed zip file.

Determine whether the destination path toFolder is a file or a folder:
If it is a file, write Add in the method described in specifying a single file or multiple files; If it is a folder, write Add in the way described in all files under the specified folder


shutil

Here is a summary of how to use shutil to archive the specified files or all files in the specified folder to specify the output format.

import shutil

fromPath = 'DataAnalysis/Pandas'
toPath = './Test/output'

shutil.make_archive(toPath, 'zip', fromPath)

Define the path of the folder to be archived (which can also be the path of single or multiple files) and the target path to store archive files.

decompression

Here is a summary of how to use the third-party library mentioned above to extract the corresponding files / folders.

zipfile

Specifies the specified files or all files in the compressed package

fromPath = './Test/output.zip'
toPath = './ZIP'
pwd = 'password'

if not os.path.exists(toPath):
    os.mkdir(toPath)

with zipfile.ZipFile(fromPath, 'r') as Import:
	# check file list of target zip file; <class 'list'> 
	print('File List: ', Import.namelist()) 
	
	# read first file in file list  
	print(Import.read(Import.namelist()[0]))   
	
	# unzip 2nd file in namelist() to path  
	Import.extract(path=toPath, member=Import.namelist()[1], pwd=pwd)
	
	# unzip all in namelist() to path    
    z.extractall(path=toPath, pwd=pwd)                                                       

After reading fromPath, you can print out output as a list through namelist() All file names in the zip file

You can use the namelist to see which files are in the compressed file.
You can read the contents of the file existing in namelist() through read().

extract(member=Import.namelist()[x]) can be used to extract single or multiple files to the specified current path toPath, or extract all files to the above path.

Reference link

When writing this article, refer to the following links:

python separates file names and paths as well as file names and suffixes
File compression and decompression using Python
Use of zipfile and tarfile compressed file modules in python
[zipfile] Python packages files into zip packages & decompresses them
python zipfile package folder, compressed folder as zip package
Python 3 error: permissionerror: [errno 13] how to resolve permission denied? "xsl, csv" was successfully resolved
Python error: permissionerror: [errno 13] detailed explanation of permission denied solution
shutil -- high level file operation ¶

Keywords: Python

Added by boon_chong on Sun, 09 Jan 2022 09:45:07 +0200