Automating a file backup with a Python script
Python call that copies a file to a backup folder. Put a datetime timestamp in the new name so old backups are not overwritten, and a scheduler can then run the script.
Understanding Automating a file backup with a Python script in WA Computer Science ATAR Unit 1
What it does: os.listdir returns the names of the items in a folder, so a for loop can copy each file in turn. A try/except block handles a failed copy. The except line names the error to catch, so a scheduled run can continue or report the problem. shutil.copy needs two arguments: the source file and the destination. A new name each run keeps every earlier backup. The original file is not touched. The datetime module gives the current date and time. strftime formats it as text. os.listdir has nothing to do with time. Slashes are used in file paths, so a name containing them breaks the path. Use hyphens or underscores.
Common mistakes: The import order is wrong. Python uses 'from module import name', so it should be 'from datetime import datetime'. This gives the datetime class you need for timestamped backup names. The arguments are the wrong way round. shutil.copy takes the source first, then the destination, so it should be shutil.copy(src, dst). Otherwise the backup tries to copy over the original.
Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.
This module's key terms are also in a printable crossword (Modules 315–470) and word search (Modules 100–470).
Take this worksheet further — free with Cloodit
Each concept is reinforced from every angle — learned, then applied, then tested.
Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.
Over 100 of them, across seven natural biomes.
Turns their work into revision material ready for Flash Cards and Exam study.
Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.
Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.
Quick Questions
Match the Pairs
Key Term
shutil.copy(src, dst)Fill in the Blanks
Spot the Error
Student with a class code? Join your class →