Compressed files are a common file format in daily data processing and file management. Using Python can conveniently automate compressed files, including compressing and decompressing files in various formats, such as ZIP, TAR, GZ, etc. This article will introduce in detail how to use Python to process these compressed files, covering basic operations, common libraries and their application scenarios, and provide corresponding sample code.
Why use Python to process compressed files
Automation processing: You can write scripts to automate the compression and decompression tasks, reduce manual operations and improve work efficiency.
Cross-platform: Python has good cross-platform compatibility and can handle compressed files on different operating systems.
Rich library support: Python has multiple powerful libraries to support processing of various compressed file formats, such as zipfile, tarfile, shutil, etc.
Use the zipfile module to process ZIP files
The zipfile module is a built-in module for processing ZIP files in Python, which supports creating, reading, writing and decompressing ZIP files.
Read ZIP files
Use the zipfile module to easily read the contents of ZIP files.
import zipfile # Open the ZIP filewith ('', 'r') as zip_ref: # List all files in the ZIP file zip_ref.printdir() # Unzip all files in the ZIP file zip_ref.extractall('extracted_files')
Create a ZIP file
You can use the zipfile module to create a new ZIP file and add files to it.
import zipfile # Create a ZIP filewith ('new_archive.zip', 'w') as zip_ref: # Add files to ZIP files zip_ref.write('') zip_ref.write('')
Add files to existing ZIP files
You can use the zipfile module to add files to an existing ZIP file.
import zipfile # Add files to existing ZIP fileswith ('existing_archive.zip', 'a') as zip_ref: zip_ref.write('')
Use the tarfile module to process TAR files
The tarfile module is a built-in module for processing TAR files in Python. It supports creating, reading, writing and decompressing TAR files.
Read TAR file
Use the tarfile module to easily read the contents in the TAR file.
import tarfile # Open the TAR filewith ('', 'r') as tar_ref: # List all files in the TAR file tar_ref.list() # Unzip all files in the TAR file tar_ref.extractall('extracted_files')
Create a TAR file
You can use the tarfile module to create a new TAR file and add files to it.
import tarfile # Create a TAR filewith ('new_archive.tar', 'w') as tar_ref: # Add files to the TAR file tar_ref.add('') tar_ref.add('')
Add files to existing TAR files
You can use the tarfile module to add files to an existing TAR file.
import tarfile # Add files to existing TAR fileswith ('existing_archive.tar', 'a') as tar_ref: tar_ref.add('')
Use shutil module to process compressed files
The shutil module provides advanced file operation functions, including processing of compressed files, and supports the creation and decompression of files in ZIP and TAR formats.
Create a compressed file
Use the shutil module to create compressed files easily.
import shutil # Create a ZIP fileshutil.make_archive('archive', 'zip', 'folder_to_compress') # Create a TAR fileshutil.make_archive('archive', 'tar', 'folder_to_compress')
Unzip the compressed file
Use the shutil module to easily decompress compressed files.
import shutil # Unzip the ZIP fileshutil.unpack_archive('', 'extracted_files') # Unzip the TAR fileshutil.unpack_archive('', 'extracted_files')
Practical application examples
Automatically backup folders
Here is an example of automatically backing up folders, using the zipfile module to compress the specified folder into a ZIP file and save it to a specified location.
import os import zipfile from datetime import datetime def backup_folder(folder_path, backup_path): # Get the current time as part of the backup file name timestamp = ().strftime('%Y%m%d%H%M%S') backup_file = (backup_path, f'backup_{timestamp}.zip') # Create a ZIP file with (backup_file, 'w') as zip_ref: for foldername, subfolders, filenames in (folder_path): for filename in filenames: file_path = (foldername, filename) zip_ref.write(file_path, (file_path, folder_path)) print(f'Backup completed: {backup_file}') #User Examplebackup_folder('folder_to_backup', 'backup_directory')
Automatically decompress and process the file
Below is an example of automatically decompressing ZIP files and processing the files in them. After decompressing, it will be simple to process each file (such as printing the file contents).
import os import zipfile def extract_and_process(zip_file, extract_to): # Unzip the ZIP file with (zip_file, 'r') as zip_ref: zip_ref.extractall(extract_to) # Process the decompressed file for foldername, subfolders, filenames in (extract_to): for filename in filenames: file_path = (foldername, filename) with open(file_path, 'r') as file: print(f'Processing {filename}...') content = () print(content)#User Exampleextract_and_process('', 'extracted_files')
Summarize
This article details how to use Python to automate compressed files, including reading, creating, adding, and decompressing ZIP and TAR files. By using Python's built-in zipfile, tarfile and shutil modules, developers can efficiently manage compressed files and automate file processing. The article provides rich sample code showing how to use these modules for file backup and decompression operations in practical applications. Mastering these technologies can not only improve work efficiency, but also simplify daily file management tasks.
The above is the detailed content of the detailed guide for Python processing compressed files. For more information about Python processing compressed files, please pay attention to my other related articles!