Downloads folder organiser
A script that sorts a messy folder into Images, Documents, Videos and more, safely and with an undo log.
What you'll learn
- โWorking with files using pathlib
- โMoving files safely with shutil
- โLogging and dry-run modes
What you'll build
- โSort files by extension into folders
- โNever overwrite: renames duplicates
- โ--dry-run shows what would happen
- โUndo using the move log
Step-by-step guide
- 1
Map extensions to folders
Create a dict like {"Images": [".jpg", ".png"], "Documents": [".pdf", ".docx"], ...} and invert it to look up a folder by extension.
- 2
List files
Use Path(folder).iterdir() and skip sub-folders and hidden files.
- 3
Move safely
Create the target folder with mkdir(exist_ok=True). If a file with the same name exists, add " (1)", " (2)" before the extension.
- 4
Add a dry run
With argparse, add --dry-run that only prints "file โ folder" without moving anything.
- 5
Log every move
Append each move as a JSON line {"from": ..., "to": ...} to moves.log.
- 6
Implement undo
Read moves.log backwards and move each file back, then clear the log.
- 7
Schedule it
Run it daily with Windows Task Scheduler or cron (0 21 * * * python organise.py ~/Downloads).
๐ Make it stand out
- +Watch the folder live with the watchdog library
- +Sort photos by the date they were taken (EXIF)
- +Detect duplicate files by hash