![](https://media.journoportfolio.com/users/353362/images/b98b5a4b-5de5-47a8-9a93-c2838ce96a41.jpg)
← Back
Automation | Python Script
I've been exploring using LLM's to automate tasks using python,. This is a simple script that renames all the files by date, adds a unique identifier, and puts all the different file types in a separate folder. I didn't make any modifications to the code, the LLM wrote it by itself. It took some iteration and about 30 minutes.
import os import datetime import shutil def rename_and_move_files_in_directory(): # Get the current working directory current_directory = os.getcwd() files = os.listdir(current_directory) for filename in files: # Skip directories if os.path.isdir(filename): continue # Extract the creation date of the file creation_date = datetime.datetime.fromtimestamp(os.path.getctime(filename)) formatted_date = creation_date.strftime(''%m_%d_%Y_%H%M%S'') # Extract file extension file_extension = os.path.splitext(filename)[1] if not file_extension: # Skip files without extensions continue # Construct the new filename new_filename = f"{formatted_date}{file_extension}" # Check if a file with the desired name already exists and handle it counter = 1 while os.path.exists(new_filename): new_filename = f"{formatted_date}_{counter}{file_extension}" counter += 1 # Rename the file os.rename(filename, new_filename) # Create a folder for the file type if it doesn't exist and move the file there folder_name = file_extension[1:] # Remove the dot from the extension if not os.path.exists(folder_name): os.makedirs(folder_name) shutil.move(new_filename, os.path.join(folder_name, new_filename)) rename_and_move_files_in_directory()
Post a comment