Hello Medion Community, I hope you're all doing well! I've been working on a Python script to automate file organization in my PC, and I could use some help. Goal: The script should: Scan a specified directory. Move files into subfolders based on their file extension (e.g., .jpg files into an "Images" folder). My Attempt: Here's what I have so far: import os
import shutil
def organize_files(directory):
for filename in os.listdir(directory):
if os.path.isfile(os.path.join(directory, filename)):
file_extension = filename.split('.')[-1]
target_folder = os.path.join(directory, file_extension)
if not os.path.exists(target_folder):
os.makedirs(target_folder)
shutil.move(os.path.join(directory, filename), target_folder)
if __name__ == "__main__":
directory_path = "C:/Users/YourUsername/Downloads"
organize_files(directory_path) Issue: The script works initially, but I run into issues when trying to move certain files, particularly those with similar names or special characters. Sometimes it crashes or doesn't move all the files correctly LiteBlue USPS Question: What improvements can I make to ensure all files are correctly organized without errors? Any advice on handling files with special characters or similar names would be greatly appreciated. Thanks in advance for your help! Best, Alice
... View more