Automating a file backup with a Python script

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Automating a file backup with a Python script

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line copies notes.txt into a backup folder?
A shutil.copy("notes.txt", "backup/")
B shutil.copy("backup/")
2. What is the correct argument order for shutil.copy?
A shutil.copy(dst, src)
B shutil.copy(src, dst)
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
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")
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

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1shutil.copy("notes.txt", "backup/notes.txt") ABuilds a timestamp string such as 20260315_143005 to put in a backup file name.
2datetime.now().strftime("%Y%m%d_%H%M%S") BCopies the file notes.txt into the backup folder, leaving the original file in place.
3List every file in the folder so the script can loop over them and back each one up. CThe 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.
4Catch an error, such as a missing file, so the backup script prints a message instead of crashing. Dexcept FileNotFoundError:
5shutil.copy("backup/a.txt", "a.txt") Efor name in os.listdir("docs"):
6newName = "backup/report.txt" shutil.copy("report.txt", newName) FThe backup name has no timestamp, so every run writes to the same name and overwrites the previous backup.

Definitions

shutil.copy(src, dst)

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
import ______
2.
shutil.____(src, dst)
3.
for name in os._______("reports"):
4.
ts = datetime.now().________("%Y%m%d")
5.
try: shutil.copy(src, dst) ______ FileNotFoundError:

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A import shutil
B import datetime from datetime
C now = datetime.now()
D print(now)
Question 2
A import shutil
B src = 'notes.txt'
C dst = 'backup/notes.txt'
D shutil.copy(dst, src)
Question 3
A import os
B files = os.listdir['docs']
C for f in files:
D print(f)
Question 4
A from datetime import Datetime
B stamp = datetime.now()
C t = stamp.strftime('%Y%m%d')
D print(t)
Question 5
A import shutils
B import os
C os.makedirs('backup')
D shutil.copy('a.txt','backup')

Answer Key

Automating a file backup with a Python script
Section A: Quick Questions
Q1
A — shutil.copy("notes.txt", "backup/")
Q2
B — shutil.copy(src, dst)
Q3
A — Each backup gets a unique name, so old backups are not overwritten
Q4
B — datetime.now().strftime("%Y%m%d_%H%M")
Q5
A — Returns a list of the names of the items in the docs folder
Section B: Match the Pairs
Q1
shutil.copy("notes.txt", "backup/notes.txt") → Copies the file notes.txt into the backup folder, leaving the original file in place.
Q2
datetime.now().strftime("%Y%m%d_%H%M%S") → Builds a timestamp string such as 20260315_143005 to put in a backup file name.
Q3
List every file in the folder so the script can loop over them and back each one up. → for name in os.listdir("docs"):
Q4
Catch an error, such as a missing file, so the backup script prints a message instead of crashing. → except FileNotFoundError:
Q5
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.
Q6
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.
Section C: Definitions
Q1
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.
Section D: Fill in the Blanks
Q1
import shutil
Q2
shutil.copy(src, dst)
Q3
for name in os.listdir("reports"):
Q4
ts = datetime.now().strftime("%Y%m%d")
Q5
try: shutil.copy(src, dst) except FileNotFoundError:
Section E: Spot the Error
Q1
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.
Q2
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.
Q3
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.
Q4
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.
Q5
A — The module is called shutil, not shutils. It should be 'import shutil'. Otherwise Python raises a ModuleNotFoundError and the copy never runs.