Can you create a python3 script for me that would work to create a zip archive of the…
- User
Can you create a python3 script for me that would work to create a zip archive of the files created the previous day in an identified folder and that would delete the oldest zip files from this identified folder as soon as the linux system only has 1GB of memory left? 'disk space ?
- Assistant
Certainly! Here's a Python3 script that creates a zip archive of the files created the previous day in the specified folder. It also deletes the oldest zip files when the system's available disk space is below 1GB:
import os import zipfile from datetime import datetime, timedelta import shutil import psutil # Specify the folder containing the files to be archived folder_path = '/path/to/folder' # Specify the location to store the zip archives archive_folder = '/path/to/archive' # Specify the maximum disk space limit in MB disk_space_limit = 1000 # Calculate the date of the previous day previous_day = datetime.now() - timedelta(days=1) previous_day_string = previous_day.strftime('%Y-%m-%d') # Create a list to store the zip filenames zip_filenames = [] # Iterate through files in the folder and add them to the zip_filenames list for file_name in os.listdir(folder_path): file_path = os.path.join(folder_path, file_name) if os.path.isfile(file_path): creation_date = datetime.fromtimestamp(os.path.getctime(file_path)) if creation_date.strftime('%Y-%m-%d') == previous_day_string: zip_filenames.append(file_path) # Create a zip archive using the identified files zip_filename = os.path.join(archive_folder, f'archive_{previous_day_string}.zip') with zipfile.ZipFile(zip_filename, 'w') as zip_file: for file_path in zip_filenames: zip_file.write(file_path, os.path.basename(file_path)) # Delete the oldest zip files if disk space is below the limit while psutil.disk_usage('/').free < disk_space_limit * 1024 * 1024: oldest_zip = min((os.path.join(archive_folder, file_name) for file_name in os.listdir(archive_folder) if file_name.endswith('.zip')), key=os.path.getctime) os.remove(oldest_zip)
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.