Intermediate level · Module 5: Automating Everyday Office Tasks with Python · Lesson 1 of 4
Organising Files and Folders Automatically
Write scripts that rename, sort and back up files safely.
In this lesson you will
- Find files by pattern and move or copy them
- Build a dry-run mode before changing files
Key concepts
globshutil.copy/movedry runbackup before change
The lesson
Repetitive file work — sorting downloads, renaming scanned forms, archiving old reports — is ideal for automation. pathlib lists files, reads their extensions and moves them; shutil copies and moves whole folders.
Anything that moves or deletes files must be safe. Add a dry-run mode that prints what would happen without changing anything, and run it first. Never delete originals until you have checked the result.
Decide on rules before coding: which extensions go to which folder, what happens to unknown types, and what to do if a file with the same name already exists.
Step by step
- Write a dictionary mapping extensions to folder names.
- List the files in the messy folder with Path.iterdir().
- Print the planned moves in dry-run mode.
- Run for real and check that every file arrived in the right folder.
Practice activity
Write a script that sorts a messy Downloads-style folder into subfolders by file type, with a --dry-run option that only prints planned moves.
Expected result: A folder correctly sorted, with the dry run matching the real run.
Check your understanding
-
Why build a dry-run mode for file automation?
Show answer
To see exactly what will change before any file is moved or deleted.
Taught live by an instructor — online or in person. Free course lessons are for self-study and do not lead to a certificate — see registration options.