High-Level File Operations (shutil)
The shutil module provides high-level file operations — copying, moving, archiving, and deleting whole files and directory trees — in a single, convenient function call each.
Learn High-Level File Operations (shutil) in our free Python course — an interactive lesson with runnable examples, a practice exercise and a quick reference.
Part of the free Python course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
Where os gives you low-level building blocks, shutil gives you the "do what I mean" verbs: copy a folder, zip a directory, move a file, free-space check. We'll demo everything safely inside a temporary directory.
shutil.copy(src, dst) duplicates a file's contents and permissions; copy2 additionally preserves timestamps and metadata. We work inside a TemporaryDirectory so nothing is left behind:
These three handle entire directory trees. copytree recursively copies a folder, move relocates or renames it, and rmtree deletes it and everything inside:
make_archive zips a folder and returns the new archive's path; unpack_archive extracts it again. Two handy utilities round things out: disk_usage reports free space, and which locates an executable on PATH:
Replace each ___ so the code makes a backup copy of a config file and confirms it matches.
❌ copytree onto an existing folder (older Python)
❌ Adding the extension to make_archive yourself
Build a tiny "site", archive it, copy it to a release folder, then clean up the source — all inside a temp directory.
Lesson complete — you wrangle files like a pro!
You can copy files with copy / copy2 , copy and delete whole trees with copytree / rmtree , relocate with move , zip and unzip with make_archive / unpack_archive , and check space and tools with disk_usage and which .
🚀 Up next: socket — talk to other machines over the network.
Practice quiz
What does shutil.copy2 preserve that shutil.copy does not?
- The file contents
- The file permissions
- Timestamps and metadata
- Nothing different
Answer: Timestamps and metadata. Both copy contents and permission bits; copy2 additionally preserves timestamps and metadata.
What happens if you pass a directory to shutil.copy?
- It raises an error (IsADirectoryError)
- It copies the whole tree
- It copies only the first file
- It creates an empty folder
Answer: It raises an error (IsADirectoryError). copy and copy2 work on single files only; use copytree for directories.
Which function recursively copies a directory and everything inside it?
- shutil.copy
- shutil.copyfile
- shutil.duplicate
- shutil.copytree
Answer: shutil.copytree. shutil.copytree(src, dst) recursively copies a folder and all its contents.
What does shutil.rmtree(path) do?
- Moves a directory
- Permanently deletes a directory and all its contents
- Empties a file
- Renames a directory
Answer: Permanently deletes a directory and all its contents. rmtree recursively deletes a directory tree permanently — no recycle bin, no undo.
Which function moves or renames a file or directory?
- shutil.move
- shutil.rename
- shutil.shift
- shutil.relocate
Answer: shutil.move. shutil.move(src, dst) relocates or renames files and whole directory trees.
What does shutil.make_archive("backup", "zip", folder) return?
- None
- The folder contents
- The path to the created archive (e.g. backup.zip)
- True or False
Answer: The path to the created archive (e.g. backup.zip). It builds the archive and returns its full path, adding the .zip extension for you.
How should you name the first argument to make_archive?
- With the extension, e.g. "backup.zip"
- Without the extension, e.g. "backup"
- As a folder path only
- It must be empty
Answer: Without the extension, e.g. "backup". Leave the extension off — shutil adds it. "backup.zip" would produce backup.zip.zip.
Which function extracts an archive back into a directory?
- shutil.extract
- shutil.unzip
- shutil.open_archive
- shutil.unpack_archive
Answer: shutil.unpack_archive. shutil.unpack_archive(filename, extract_dir) reverses make_archive.
What does shutil.disk_usage(path) return?
- A list of files
- A named tuple of total, used, and free bytes
- The folder size only
- A boolean
Answer: A named tuple of total, used, and free bytes. It reports total, used, and free bytes for the filesystem containing that path.
What does shutil.which("python3") do?
- Runs python3
- Installs python3
- Returns the full path of the executable on PATH, or None
- Lists Python versions
Answer: Returns the full path of the executable on PATH, or None. which searches PATH for an executable and returns its location, like the Unix 'which' command.