Automate File Organisation with Python
There are nine hundred and fourteen files in the average Downloads folder. That is not a made-up number. It is what came out of a survey on a programming forum in 2024. The median was four hundred. Nobody had fewer than eighty. Nobody was happy about it.
The problem is not laziness
The Downloads folder collects everything because that is what it is told to do. Browsers default to it. Slack defaults to it. Email attachments land there. And because sorting files by hand is neither urgent nor interesting, it does not happen until the folder contains so many items that finding anything takes longer than re-downloading it.
The standard advice is to sort manually, once a week, into a folder structure you maintain by hand. That advice has a compliance rate of approximately zero over any period longer than a month. The alternative is a script that does it every time the computer starts up, in about two seconds, without being asked.
What the script does
The basic version is twelve lines of Python. It reads every file in the Downloads folder, checks the extension, and moves it to a subfolder named after the file type. PDFs go to Downloads/documents. Images go to Downloads/images. Zip files go to Downloads/archives. Anything it does not recognise stays where it is.
Twelve lines. No libraries beyond what ships with Python. It runs in under a second on a folder with two thousand files, which is more than most people will ever accumulate.
The mapping between extensions and folders is a dictionary, a list of pairs that says '.pdf goes here, .jpg goes there.' Adding a new rule is one line. Removing one is deleting that line. There is nothing else to configure.
Renaming is the harder problem
Sorting files by type is useful. Renaming them is where the real time savings appear, because badly named files are the reason the sorted folders still feel unusable.
A folder full of document(1).pdf, document(2).pdf and IMG_20240318_142756.jpg is organised by type and useless by name. The rename script fixes this by applying a pattern: date first, then a readable slug derived from the original name or the folder it came from.
IMG_20240318_142756.jpg becomes 2024-03-18-photo.jpg. If there are four photos from that day, they become 2024-03-18-photo-1.jpg through 2024-03-18-photo-4.jpg. The pattern is predictable and it sorts chronologically in every file manager.
The dangerous part of any rename script is that it is irreversible if you get it wrong. The version in the book writes a log file before it touches anything, a plain text list of 'old name → new name', so that undoing a bad run is one more script rather than an afternoon of regret.
Running it without thinking about it
A script you have to remember to run is a script you will stop running. The fix is to schedule it.
On Windows, Task Scheduler can run a Python script every time you log in, or every hour, or every Tuesday at 6am. On Mac, a launchd plist does the same thing. On Linux, it is a cron job. All three take about five minutes to set up and then never need to be thought about again.
The book walks through all three, but the principle is the same everywhere: the script should run before you notice the problem, not after you have spent ten minutes looking for a file you downloaded yesterday.
Where this connects to everything else
File organisation is usually the first automation people build because the pain is visible and the fix is immediate. But the same patterns (reading a directory, checking a condition, moving or renaming something) show up in almost every other script you will write.
The spreadsheet scripts use the same loop structure to walk through rows instead of files. The logic is identical: look at each item, decide what to do with it, do it. Once you have built one, the second is mostly copy and paste.
If the data you need does not already exist on your machine, if it lives on a web page and you are copying it by hand, that is a different problem with the same shape, covered in pulling data from the web without writing code by hand.
Start with the Downloads folder
Do not try to organise your entire hard drive on the first pass. Start with one folder, the one that is most obviously out of control, and write the twelve-line sort script for that. Run it once by hand. Watch it move four hundred files in a second and a half.
That is the moment where automation stops being an abstract concept and starts being a tool. Every script after this one is easier, because the question is no longer 'can I do this?' but 'is this one worth the twenty minutes?'
If this was useful
Automate Office Tasks with Python
You spend an hour every week doing something a script could do in four seconds, and you have never written a script because every tutorial assumes you want to become a software engineer.
Get it on Gumroad$17 USD