Create a python script that sorts files on the desktop into separate folders based on…
- User
Create a python script that sorts files on the desktop into separate folders based on their file type. The script should prompt the user for the source and destination paths, and then guide the user through the process of setting up the file types dictionary and running the sorting code.
- Assistant
Sure, here's an example Python script that sorts files on the desktop into separate folders based on their file type:
import os import shutil # Prompt the user for the source and destination paths source_path = input("Enter the source path: ") destination_path = input("Enter the destination path: ") # Define a dictionary of file types and their corresponding folder names file_types = { ".txt": "Text Files", ".docx": "Word Documents", ".jpg": "JPEG Images", ".pdf": "PDF Documents" } # Create the destination folders for folder_name in file_types.values(): folder_path = os.path.join(destination_path, folder_name) os.makedirs(folder_path, exist_ok=True) # Loop through the files in the source directory for file_name in os.listdir(source_path): file_path = os.path.join(source_path, file_name) if os.path.isfile(file_path): # Get the file extension and corresponding folder name file_extension = os.path.splitext(file_name)[1].lower() if file_extension in file_types: folder_name = file_types[file_extension] # Move the file to the corresponding destination folder destination_folder = os.path.join(destination_path, folder_name) shutil.move(file_path, destination_folder) print(f"Moved {file_name} to {destination_folder}") - User
How to customize it to any file types I want? Can you provide me the code for that too since I am not very good at using python. Also just to be sure, it wont mess up the sorting process right? I don't want two different file types in the same folder
- Assistant
Sure, here is the code to customize the script to move files based on any file types you want:
import os import shutil # Prompt the user for the source and destination paths source_path = input("Enter the source path: ") destination_path = input("Enter the destination path: ") # Define a dictionary of file types and their corresponding folder names file_types = {} while True: extension = input("Enter a file extension (or press Enter to finish): ") if extension == "": break folder_name = input("Enter a folder name for the extension: ") file_types[extension] = folder_name # Create the destination folders for folder_name in file_types.values(): folder_path = os.path.join(destination_path, folder_name) os.makedirs(folder_path, exist_ok=True) # Loop through the files in the source directory for file_name in os.listdir(source_path): file_path = os.path.join(source_path, file_name) if os.path.isfile(file_path): # Get the file extension and corresponding folder name file_extension = os.path.splitext(file_name)[1].lower() if file_extension in file_types: folder_name = file_types[file_extension] # Move the file to the corresponding destination folder destination_folder = os.path.join(destination_path, folder_name) shutil.move(file_path, destination_folder) print(f"Moved {file_name} to {destination_folder}")
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)