Batch-rename files with pathlib
The program creates its own fake inbox first. Each run starts in a fresh empty folder, so it is repeatable.
Automation scripts do boring work reliably: they rename and move files with pathlib, handle dates and time zones correctly with datetime and zoneinfo, and run other programs with subprocess.run using a list of arguments, a timeout and a return-code check, never a shell built from user input.
Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.
Ready for more? This phase has 6 hands-on exercises with automatic checks.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Write archive_name(filename) that returns the lower-case name with spaces replaced by underscores, keeping the extension.
Write to_utc_string(text) that takes an ISO time with an offset such as 2026-10-10T08:15:00+05:30 and returns it in UTC in the same format.
Write (non-runnable) a function git_branch() that returns the current git branch using subprocess.run with a list, check=True and a timeout.
Explain it without notes
How do you make a file-renaming script safe?
What is the difference between naive and aware datetimes, and which should you store?
Why is subprocess.run(command_string, shell=True) dangerous with untrusted input, and what is the safe alternative?
What do check=True and timeout= do in subprocess.run?
Expected output
Report Feb.txt -> archive/report_feb.txt
Report Jan.TXT -> archive/report_jan.txt
notes.md -> archive/notes.md
photo 1.JPG -> archive/photo_1.jpg
inbox now: []
text files: ['report_feb.txt', 'report_jan.txt']
all sizes: {'notes.md': 13, 'photo_1.jpg': 16, 'report_feb.txt': 19, 'report_jan.txt': 19}