← WA Computer Science ATAR Unit 1

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.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

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.

Learning
Duo
Which line copies notes.txt into a backup folder?
shutil.copy("notes.txt", "backup/")shutil.copy("backup/")
Matches
shutil.copy("backup/a.txt", "a.txt")
↓
The arguments are the wrong way round. The destination is given first and the source second, so the copy goes from the backup folder back to the original, not into the backup.
Applying
Meanings
Term
shutil.copy(src, dst)
Meaning
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.
Practice
import os
Testing
Gaps
import
Check Answer
Spots
1import shutil
2import datetime from datetime

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Golden Eagle' badge — High Flyer, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. Which line copies notes.txt into a backup folder?
A. shutil.copy("notes.txt", "backup/") ✓
B. shutil.copy("backup/")
shutil.copy needs two arguments: the source file and the destination.
2. What is the correct argument order for shutil.copy?
A. shutil.copy(dst, src)
B. shutil.copy(src, dst) ✓
The source (the file you have) comes first. The destination (where the copy goes) comes second.
3. Why add a datetime timestamp to the backup file name?
A. Each backup gets a unique name, so old backups are not overwritten ✓
B. It makes the original file delete itself after copying
A new name each run keeps every earlier backup. The original file is not touched.
4. Which code builds a timestamp to use in a file name?
A. os.listdir().strftime("%Y%m%d")
B. datetime.now().strftime("%Y%m%d_%H%M") ✓
The datetime module gives the current date and time. strftime formats it as text. os.listdir has nothing to do with time.
5. What does os.listdir("docs") do in a backup script?
A. Returns a list of the names of the items in the docs folder ✓
B. Copies every file in docs to the backup folder
os.listdir only lists names. You then loop through them and call shutil.copy on each one.

Match the Pairs

shutil.copy("notes.txt", "backup/notes.txt") → Copies the file notes.txt into the backup folder, leaving the original file in place.
datetime.now().strftime("%Y%m%d_%H%M%S") → Builds a timestamp string such as 20260315_143005 to put in a backup file name.
List every file in the folder so the script can loop over them and back each one up. → for name in os.listdir("docs"):
Catch an error, such as a missing file, so the backup script prints a message instead of crashing. → except FileNotFoundError:
shutil.copy("backup/a.txt", "a.txt") → The arguments are the wrong way round. The destination is given first and the source second, so the copy goes from the backup folder back to the original, not into the backup.
newName = "backup/report.txt" shutil.copy("report.txt", newName) → The backup name has no timestamp, so every run writes to the same name and overwrites the previous backup.

Key Term

shutil.copy(src, dst)
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.

Fill in the Blanks

1. import shutil
Hint: Type the name of the module that provides file-copying functions.
2. shutil.copy(src, dst)
Hint: Type the function name that copies a file from the source to the destination.
3. for name in os.listdir("reports"):
Hint: Type the os function that returns the names of the files in a folder.
4. ts = datetime.now().strftime("%Y%m%d")
Hint: Type the method that formats a date and time as text for a file name.
5. try: shutil.copy(src, dst) except FileNotFoundError:
Hint: Type the keyword that catches an error raised inside the try block.

Spot the Error

Question 1
A import shutil
B import datetime from datetime
C now = datetime.now()
D print(now)
Line B — 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.
Question 2
A import shutil
B src = 'notes.txt'
C dst = 'backup/notes.txt'
D shutil.copy(dst, src)
Line D — 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.
Question 3
A import os
B files = os.listdir['docs']
C for f in files:
D print(f)
Line B — os.listdir is a function, so it is called with round brackets, not square brackets. It should be os.listdir('docs'), which returns a list of the file names in the folder.
Question 4
A from datetime import Datetime
B stamp = datetime.now()
C t = stamp.strftime('%Y%m%d')
D print(t)
Line A — Python is case-sensitive. The class is datetime with a lowercase d, so the import should be 'from datetime import datetime'. Otherwise datetime.now() on the next line fails.
Question 5
A import shutils
B import os
C os.makedirs('backup')
D shutil.copy('a.txt','backup')
Line A — The module is called shutil, not shutils. It should be 'import shutil'. Otherwise Python raises a ModuleNotFoundError and the copy never runs.

Student with a class code? Join your class →