1. First, the modules that need to be used are imported:
- `zipfile`: The module used to process ZIP files.
- `os`: Provides the function to interact with the operating system.
- `shutil`: used for advanced file operations, such as moving files, etc.
- `datetime`: The module used to handle dates and times.
2. Define a function `unzip_todays_files(zip_files_folder, extract_folder)`, which accepts two parameters:
- `zip_files_folder`: The path to the folder containing the ZIP file. - `extract_folder`: Decompress the path to the target folder.
3. Get the current date
today = ().date()
Here, use `().date()` to get the current date.
4. Iterate through all ZIP files in the specified folder:
for file_name in (zip_files_folder): if file_name.endswith('.zip'): ...
Use `(zip_files_folder)` to get all file names in the specified folder and then check whether the file names end in `.zip` to filter out the ZIP files.
5. Check whether the ZIP file was created on the same day
create_date = ((zip_file_path)).date() if create_date == today: ...
Use `(zip_file_path)` to get the time of creation of the file and convert it to a date. Next, compare it with the current date to determine if the file was created on the same day.
6. Unzip the ZIP file:
with (zip_file_path, 'r') as zip_ref: ...
Use `` to open the ZIP file and specify it as read-only mode. Use the `with` context manager to ensure that the file is closed when the context is exited.
7. Get the unzipped file list:
extracted_files = zip_ref.namelist()
Use `zip_ref.namelist()` to get a list of all file names in the ZIP file.
8. Judging the storage directory based on the specific information in the file name:
for extracted_file in extracted_files: if 'YYFX' in extracted_file: target_folder = extract_folder2 elif 'Other specific information' in extracted_file: target_folder = 'Target folder path 2' else: target_folder = extract_folder # Store to the decompression target folder by default
Based on the specific information in the file name, determine which target folder the file should be decompressed. If the file name contains 'YYFX', the file is extracted to `extract_folder2`; if the file name contains other specific information, the file is extracted to another target folder; otherwise, the file is extracted to `extract_folder` by default.
9. Make sure the target folder exists:
(target_folder, exist_ok=True)
Use `(target_folder, exist_ok=True)` to create a target folder if the target folder does not exist.
10. Unzip the file to the target folder:
zip_ref.extract(extracted_file, target_folder)
Use `zip_ref.extract(extracted_file, target_folder)` to extract the file into the target folder.
11. Print the decompression information:
print(f"Unzipped file: {extracted_file} Go to the destination folder: {target_folder}")
Prints the unzipped file name and destination folder path.
Complete code
import zipfile import os import shutil import datetime def unzip_todays_files(zip_files_folder, extract_folder): # Get the current date today = ().date() # traverse all ZIP files in the specified folder for file_name in (zip_files_folder): if file_name.endswith('.zip'): # Build the full path to the ZIP file zip_file_path = (zip_files_folder, file_name) # Get the creation date of the ZIP file create_date = ((zip_file_path)).date() # Check whether the ZIP file was created on the same day if create_date == today: print(f"Unzipping the file: {zip_file_path}") # Open the ZIP file with (zip_file_path, 'r') as zip_ref: # Get the unzipped file list extracted_files = zip_ref.namelist() #Judge which folder to store based on the specific information in the file name for extracted_file in extracted_files: #Judge storage directory based on specific information in the file name if 'YYFX' in extracted_file: target_folder = extract_folder2 elif 'Other specific information' in extracted_file: target_folder = 'Target folder path 2' else: target_folder = extract_folder # Save to the decompression target folder by default # Make sure the target folder exists (target_folder, exist_ok=True) # Unzip the file to the target folder zip_ref.extract(extracted_file, target_folder) print(f"Unzipped file: {extracted_file} Go to the destination folder: {target_folder}") # Specify the folder path containing the ZIP file and the decompression target folder pathzip_files_folder = 'C:\\Users\\Administrator\\Downloads' # Folder containing ZIP filesextract_folder = 'F:\\Work content\\My data\\Product cost analysis data' # Already existing directoryextract_folder2 = 'F:\\Work content\\My data\\Order list data' # Call the function to decompress the file created on the dayunzip_todays_files(zip_files_folder, extract_folder)
The above is the detailed content of Python decompressing the ZIP file created on the day to the specified folder. For more information about Python decompressing ZIP to the specified folder, please follow my other related articles!