It took me only 5 mins to clear all the mess
Not yet a Medium Member? READ ARTICLE HERE
If you’re like me and use your laptop/computer regularly, your Downloads folder is the place where all random files grabbed from the internet end up.
In the beginning, it’s easy to locate files, but after a while, you dread even looking at the Downloads folder.
You might even end up downloading the same file twice just to avoid the trouble of searching for it again.
Manually sorting through the mess I’ve created is out of the question — who even has the time for such tedious work? I’d rather watch paint dry.
I wrote this Python script that automatically organises your downloads folder and makes it look presentable and easy to access.
import os
import shutil
from datetime import datetime
# Define the source folder (Downloads)
source_folder = os.path.expanduser("~/Downloads")
# Define destination folders and their corresponding file extensions
destination_folders = {
"Images": [".jpg", ".jpeg", ".png", ".gif", ".bmp", ".tiff", ".webp"],
"Documents": [".pdf", ".docx", ".doc", ".txt", ".rtf", ".xlsx", ".pptx", ".csv"],
"Videos": [".mp4", ".mov", ".avi", ".mkv", ".wmv", ".flv"],
"Audio": [".mp3", ".wav", ".aac", ".flac", ".ogg"],
"Archives": [".zip", ".rar", ".7z", ".tar", ".gz"],
"Installers": [".exe", ".msi", ".dmg", ".pkg", ".deb", ".rpm"]
}
# Create a log of operations
log_file = os.path.join(source_folder, "sort_log.txt")
with open(log_file, "w") as log:
log.write(f"File sorting started at {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n\n")
# Process each file in the source folder
for file in os.listdir(source_folder):
file_path = os.path.join(source_folder, file)
# Skip folders and the log file itself
if os.path.isfile(file_path) and file != "sort_log.txt":
file_moved = False
# Check file against each category
for folder, extensions in destination_folders.items():
if any(file.lower().endswith(ext) for ext in extensions):
# Create destination folder if it doesn't exist
dest_folder = os.path.join(source_folder, folder)
os.makedirs(dest_folder, exist_ok=True)
# Move the file
dest_path = os.path.join(dest_folder, file)
shutil.move(file_path, dest_path)
log.write(f"Moved: {file} → {folder}\n")
file_moved = True
break
if not file_moved:
# Handle uncategorized files
misc_folder = os.path.join(source_folder, "Miscellaneous")
os.makedirs(misc_folder, exist_ok=True)
shutil.move(file_path, os.path.join(misc_folder, file))
log.write(f"Moved: {file} → Miscellaneous\n")
log.write(f"\nFile sorting completed at {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
Now what’s next after giving you this code, you need to have Python installed on your computer/MacBook, if not, download the latest version from python.org, as of writing this, the latest version is 3.13.2.
In my case, I am using VS Code, which I already had installed. You can also use other code editors, it would still work.
Download python from the extensions folder, it takes less than 2 minutes.(if not installed yet)
Scroll to file menu and on the drop down create new file, save as file name sort_downloads.py (note the folder where you save your file)
Paste the above code as shown in the screenshot below.
at the bottom of the above code block click on the Terminal menu and you would see a blinking text(PS C:\users \user)that shows your current working directory.
for the code to work, you need to change the directory to the place where you saved the above code on your computer.
If you saved the code on documents, use the command cd documents to change your working directory to the documents folder.
python sort_downloads.py
Paste the above code and click enter, if correct, you would see progress similar to the screenshot below.
If you encounter problems, your working directory might be the problem, or you might not have saved the sort_downloads.py file.
Optional: Automate the Script
If you want to run the script automatically at regular intervals, follow these steps:
On Windows:
- Open Task Scheduler.
- Create a new task:
- Set the trigger (e.g., daily at a specific time).
- Set the action to run the script:
- Program/script:
python
- Add arguments:
path\to\sort_downloads.py
- Save the task.
On macOS/Linux:
- Open a terminal.
- Edit the crontab file:
crontab -e
0 8 * * * /usr/bin/python3 /path/to/sort_downloads.py
Add a cron job to run the script at a specific time (e.g., daily at 8 AM)
Save and exit the editor.
Also, check out my review of the Python development course that helped me become proficient in Python!
✔️If my writing has inspired, informed, or entertained you, consider buying me a coffee. It’s a small gesture that goes a long way in keeping this creative journey alive. Thank you for your support!
Learn more How I Sorted My Downloads Folder in Minutes Using a Simple Python Script