Skip to content

Instantly share code, notes, and snippets.

@satputekuldip
Created March 6, 2024 18:26
Show Gist options
  • Save satputekuldip/81235510555e6f75cb386427d8a6333c to your computer and use it in GitHub Desktop.
Save satputekuldip/81235510555e6f75cb386427d8a6333c to your computer and use it in GitHub Desktop.
File Organizer
import os
import shutil
def organize_downloads(downloads_folder):
"""
Organizes files in the downloads folder based on their extensions.
Args:
downloads_folder: Path to the Downloads folder.
"""
# Create a dictionary to map file extensions to folder names
file_type_folder_dict = {
".pdf": "PDFs",
".docx": "Documents",
".xlsx": "Spreadsheets",
".pptx": "Presentations",
".jpg": "Images",
".png": "Images",
".jpeg": "Images",
".mp3": "Music",
".mp4": "Videos",
".mkv": "Videos",
".zip": "Archives",
".dmg": "DMG",
".rar": "Archives",
".pem": "Keys",
".cer": "Keys",
".pkg": "DMG",
".webp": "Images",
".iso": "ISO",
".log": "Text",
".txt": "Text",
".deb": "Ubuntu",
".notes": "Text",
".sh": "Scripts",
".csv": "Excel",
".xls": "Excel",
".xlsx": "Excel",
".xlsm": "Excel",
".ods": "Excel",
".numbers": "Excel",
".mov": "Videos",
".gz": "Archives",
".tar": "Archives",
".mbi": "Backup",
".img": "ISO",
".json": "JSON",
".db": "Backup",
".sql": "Backup",
".epub": "Books",
".heic": "Images",
}
# Loop through all files in the Downloads folder
for filename in os.listdir(downloads_folder):
# Get the file extension
file_extension = os.path.splitext(filename)[1].lower()
# Check if the file extension is in the dictionary
if file_extension in file_type_folder_dict:
# Create the folder if it doesn't exist
folder_name = file_type_folder_dict[file_extension]
folder_path = os.path.join(downloads_folder, folder_name)
if not os.path.exists(folder_path):
os.makedirs(folder_path)
# Move the file to the corresponding folder
source_path = os.path.join(downloads_folder, filename)
destination_path = os.path.join(folder_path, filename)
shutil.move(source_path, destination_path)
# Replace "YOUR_DOWNLOADS_FOLDER_PATH" with the actual path to your Downloads folder
downloads_folder = "/Volumes/DATA/Downloads"
organize_downloads(downloads_folder)
print("Downloads folder organized successfully!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment