Start Your Journey with Linux Command Line
| Organize your Downloads folder automatically with Python |
If you are new to Windows, nothing feels better than watching your computer finish a boring task by itself. In this tutorial you will write a real Python program that cleans up your Downloads folder: every file is sorted into a sub-folder by its type, and an activity log is saved so you can see what changed. Best of all, take a few minutes today, and from tomorrow it runs with zero clicks.
The whole setup, from installing Python to scheduling the job, takes about ten minutes.
Before any automation, Python must be on your machine.
.exe file.On the first screen of the installer, tick these checkboxes before clicking Install Now:
Install launcher for all users (recommended)Add Python to PATH (required)The second checkbox is the one most beginners miss. If Python is not on the PATH, Windows cannot find the python command when you type it.
Open Command Prompt (press Windows + R, type cmd, press Enter) and run:
python --version
If that prints nothing, try the Windows launcher instead:
py --version
Seeing a line like Python 3.13.2 means everything is installed correctly.
Good projects live in their own folder. Create one called automation:
cd %USERPROFILE%
mkdir automation
cd automation
Every script you build in this series will live here, which keeps things tidy and easy to find.
Create a new text file named organize_downloads.py inside automation. Open it in Notepad and copy this in:
import os
import shutil
from datetime import datetime
# 1. Point at the Downloads folder of the current user
source = os.path.join(os.environ["USERPROFILE"], "Downloads")
# 2. Map common extensions to tidy sub-folder names
groups = {
"Images": (".jpg", ".jpeg", ".png", ".gif", ".webp"),
"Documents": (".pdf", ".docx", ".xlsx", ".txt", ".md"),
"Archives": (".zip", ".rar", ".7z", ".tar", ".gz"),
"Videos": (".mp4", ".mov", ".avi", ".mkv"),
"Music": (".mp3", ".wav", ".flac", ".m4a"),
}
log = []
# 3. Walk every file in Downloads (no sub-folder recursion)
for name in os.listdir(source):
path = os.path.join(source, name)
if os.path.isdir(path):
continue # skip folders, only sort loose files
ext = os.path.splitext(name)[1].lower()
dest_dir = "Other" # anything without a matching group
for folder, exts in groups.items():
if ext in exts:
dest_dir = folder
break
# 4. Create the group folder if it does not exist yet
os.makedirs(os.path.join(source, dest_dir), exist_ok=True)
# 5. Move the file and record what happened
destination = os.path.join(source, dest_dir, name)
shutil.move(path, destination)
log.append(f"Moved {name} -> {dest_dir}")
# 6. Write a timestamped report file so you can audit the run
report = os.path.join(source, "organize_report.txt")
with open(report, "a", encoding="utf-8") as f:
f.write(f"\n--- Run {datetime.now().isoformat()} ---\n")
f.write("\n".join(log) if log else "Nothing to organize.")
print(f"Done. {len(log)} file(s) moved. Report saved to {report}.")
The program follows a simple plan:
os.environ.Documents and Archives.organize_report.txt, timestamped with the current date and time.Each line in the code has a short inline comment, so even if you have never read Python before, you can follow the logic line by line.
Run the automation once by hand to confirm it works:
cd %USERPROFILE%\automation
python organize_downloads.py
Check your Downloads folder. Loose files should now be grouped, and organize_report.txt should list what moved. If you see an error about python not being recognized, go back to Step 1 and confirm the Add Python to PATH checkbox.
Now the fun part: software that runs itself.
The Windows Task Scheduler runs your script on a schedule even when you are logged out:
schtasks /Create /TN "Organize Downloads" /TR "python %USERPROFILE%\automation\organize_downloads.py" /SC DAILY /ST 09:00 /F
Run that from an Administrator Command Prompt. The task runs every day at 9:00 AM.
You can also run the script by double-clicking it in File Explorer. Add this line to the very end so the window stays open after the work finishes:
input("Press Enter to close...")
Double-clicking a .py file on Windows launches it with the Python runtime directly; this is a great way to run a script when you are at the keyboard.
To run the script every time you sign in instead of on a fixed clock, add a shortcut to the script inside the Startup folder:
explorer shell:startup
Drop a shortcut to organize_downloads.py into that window and it will run at every login.
python simply will not work in the terminal.automation so you always know where it lives.groups dictionary to sort any file type you like.schtasks gives you unattended scheduling; the Startup folder gives you per-login execution.To go further, try adding rules for email drafts, renaming files by date, or backing up a folder to a second drive. The same pattern applies to all of them.
If this workflow helped you, you will love the full series. Subscribe to @CodeSecureTech and follow my Facebook page for more hands-on Python automation tutorials, scripts you can copy, and Windows tips that save real time. New workflows drop regularly, so join now and never miss one.
Comments
Post a Comment
Your opinion matters, your voice makes us proud and happy. Your words are our motivation.