From 030df28073307dd5bd9d9ef5cf1ab682e6eb2cd1 Mon Sep 17 00:00:00 2001 From: OkenHaha Date: Tue, 22 Oct 2024 16:20:11 +0530 Subject: [PATCH 1/2] Added a python script to organize files to corresponding directories --- File Organizer/file_organizer.py | 49 ++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 File Organizer/file_organizer.py diff --git a/File Organizer/file_organizer.py b/File Organizer/file_organizer.py new file mode 100644 index 0000000..1356d41 --- /dev/null +++ b/File Organizer/file_organizer.py @@ -0,0 +1,49 @@ +import os +import shutil + +# Define the directory to organize +root_dir = input("Enter Path Directory: ") + +# Define the file types and their corresponding subdirectories +file_types = { + 'images': ['.jpg', '.jpeg', '.png', '.gif', '.bmp'], + 'documents': ['.pdf', '.doc', '.docx', '.txt', '.xlsx', 'csv'], + 'videos': ['.mp4', '.mkv', '.avi', '.mov'], + 'audio': ['.mp3', '.wav', '.ogg'], + 'archives': ['.zip', '.rar', '.7z', '.tar'], + 'others': [] +} + +def organize_files(root_dir): + # Create subdirectories if they don't exist + for subdirectory in file_types.keys(): + subdirectory_path = os.path.join(root_dir, subdirectory) + if not os.path.exists(subdirectory_path): + os.makedirs(subdirectory_path) + + # Iterate through all files in the root directory + for filename in os.listdir(root_dir): + file_path = os.path.join(root_dir, filename) + + # Skip if it's a directory + if os.path.isdir(file_path): + continue + + # Get the file extension + file_extension = os.path.splitext(file_path)[1].lower() + + # Determine the subdirectory based on the file extension + for subdirectory, extensions in file_types.items(): + if file_extension in extensions: + subdirectory_path = os.path.join(root_dir, subdirectory) + shutil.move(file_path, subdirectory_path) + print(f"Moved '{filename}' to '{subdirectory}'") + break + else: + # If the file type is not recognized, move it to 'others' + subdirectory_path = os.path.join(root_dir, 'others') + shutil.move(file_path, subdirectory_path) + print(f"Moved '{filename}' to 'others'") + +if __name__ == "__main__": + organize_files(root_dir) \ No newline at end of file From f4b7c1e890df09bce36050588d5c04d410899381 Mon Sep 17 00:00:00 2001 From: OkenHaha Date: Tue, 22 Oct 2024 16:24:21 +0530 Subject: [PATCH 2/2] Added Readme --- File Organizer/README.md | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 File Organizer/README.md diff --git a/File Organizer/README.md b/File Organizer/README.md new file mode 100644 index 0000000..b31caf9 --- /dev/null +++ b/File Organizer/README.md @@ -0,0 +1,3 @@ +# File Oragnizer +The python script organizes files for a given directory which organizes according to the file type.
+You can update the different file types by editing the file_organizer.py under the variable name `file_types` \ No newline at end of file