Hi - in Python I have a csv-file that I update on a regular basis. I want to store the…
- User
Hi - in Python I have a csv-file that I update on a regular basis. I want to store the last 5 copies of the file in a subfolder. The subfolder should be updated every time I make a new update of the csv-file. Can you produce some Python code to do that?
- Assistant
Sure! Here's an example Python code that achieves what you're looking for:
import os import shutil def create_backup(csv_file, backup_folder): # Create the backup folder if it doesn't exist if not os.path.exists(backup_folder): os.makedirs(backup_folder) # Get the base filename without extension file_name = os.path.splitext(os.path.basename(csv_file))[0] # Copy the csv file to the backup folder using a new name with a timestamp shutil.copy2(csv_file, os.path.join(backup_folder, f"{file_name}_{time.strftime('%Y%m%d%H%M%S')}.csv")) # Get all csv files in the backup folder and sort them by modify time backup_files = sorted([file for file in os.listdir(backup_folder) if file.endswith(".csv")], key=lambda x: os.path.getmtime(os.path.join(backup_folder, x))) # Delete oldest backups beyond the last 5 if len(backup_files) > 5: for file in backup_files[:-5]: os.remove(os.path.join(backup_folder, file))You can use this function to create a backup of your csv file and keep only the last 5 copies in the specified subfolder. To use this code, you need to provide the path to your csv file and the path to the backup folder where you want to store the copies.
Usage example:
csv_file = "path/to/your/csv_file.csv" backup_folder = "path/to/your/backup_folder" create_backup(csv_file, backup_folder)Make sure to replace
"path/to/your/csv_file.csv"and"path/to/your/backup_folder"with the actual paths you want to use. - User
I only want one backup for each day even though I update the file more than once during the day.
- Assistant
Apologies for the confusion. Here's an updated version of the code that creates only one backup per day:
import os import shutil import datetime def create_backup(csv_file, backup_folder): # Create the backup folder if it doesn't exist if not os.path.exists(backup_folder): os.makedirs(backup_folder) # Get the base filename without extension file_name = os.path.splitext(os.path.basename(csv_file))[0] # Generate a timestamp for today's date today = datetime.date.today().strftime('%Y%m%d') # Get all the existing backups for today's date backup_files = [file for file in os.listdir(backup_folder) if file.startswith(f"{file_name}_{today}")] # If there's already a backup for today, remove it if backup_files: os.remove(os.path.join(backup_folder, backup_files[0])) # Copy the csv file to the backup folder with today's timestamp shutil.copy2(csv_file, os.path.join(backup_folder, f"{file_name}_{today}.csv")) # Limit the number of backups to the last 5 backup_files = sorted([file for file in os.listdir(backup_folder) if file.endswith(".csv")], key=lambda x: os.path.getmtime(os.path.join(backup_folder, x))) if len(backup_files) > 5: for file in backup_files[:-5]: os.remove(os.path.join(backup_folder, file))Now, this updated code will keep only one backup per day, replacing any previous backup made on the same day. The rest of the code remains the same as before.
Usage example:
csv_file = "path/to/your/csv_file.csv" backup_folder = "path/to/your/backup_folder" create_backup(csv_file, backup_folder)Make sure to replace
"path/to/your/csv_file.csv"and"path/to/your/backup_folder"with the actual paths you want to use.