Python Academy · Lesson

shutil for File Operations

Copy, move, and archive files.

Lesson 4 of 413 steps

shutil for File Operations is a free Python Academy lesson on CoddyKit. This is lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Python Academy learning path, and your progress syncs across the web and the CoddyKit app. The Python Academy course includes 4 lessons in total.

What Is shutil?

The shutil (shell utilities) module offers high-level file operations: copying, moving, deleting trees, and creating archives. It builds on os for everyday tasks.

import shutil, os

os.makedirs('/tmp/shutil_demo', exist_ok=True)
with open('/tmp/shutil_demo/a.txt', 'w') as f:
    f.write('hello')
print('created a.txt')

Copying a File

shutil.copy(src, dst) copies a file's contents and permission bits to a new location.

import shutil, os

os.makedirs('/tmp/shutil_demo', exist_ok=True)
with open('/tmp/shutil_demo/src.txt', 'w') as f:
    f.write('data')
shutil.copy('/tmp/shutil_demo/src.txt', '/tmp/shutil_demo/copy.txt')
print(os.path.exists('/tmp/shutil_demo/copy.txt'))

copy vs copy2

shutil.copy2 is like copy but also preserves metadata such as timestamps. Use it when modification times matter.

import shutil, os

os.makedirs('/tmp/shutil_demo', exist_ok=True)
with open('/tmp/shutil_demo/m.txt', 'w') as f:
    f.write('x')
shutil.copy2('/tmp/shutil_demo/m.txt', '/tmp/shutil_demo/m2.txt')
print('copied with metadata')

Copying a Whole Tree

shutil.copytree(src, dst) recursively copies an entire directory. The destination must not already exist (unless you pass dirs_exist_ok=True).

import shutil, os

os.makedirs('/tmp/tree_src/sub', exist_ok=True)
with open('/tmp/tree_src/sub/f.txt', 'w') as f:
    f.write('hi')
shutil.rmtree('/tmp/tree_dst', ignore_errors=True)
shutil.copytree('/tmp/tree_src', '/tmp/tree_dst')
print(os.path.exists('/tmp/tree_dst/sub/f.txt'))

Moving Files and Folders

shutil.move(src, dst) moves (or renames) a file or directory. It works across filesystems where os.rename would fail.

import shutil, os

os.makedirs('/tmp/move_demo', exist_ok=True)
with open('/tmp/move_demo/old.txt', 'w') as f:
    f.write('move me')
shutil.move('/tmp/move_demo/old.txt', '/tmp/move_demo/new.txt')
print(os.path.exists('/tmp/move_demo/new.txt'))

Deleting a Directory Tree

shutil.rmtree(path) removes a directory and everything inside it. Use it carefully; there is no undo.

import shutil, os

os.makedirs('/tmp/to_delete/inner', exist_ok=True)
shutil.rmtree('/tmp/to_delete')
print('exists after rmtree:', os.path.exists('/tmp/to_delete'))

ignore_errors and Safety

Passing ignore_errors=True to rmtree avoids a crash if the path is missing, handy for cleanup before a fresh run.

import shutil

shutil.rmtree('/tmp/never_existed', ignore_errors=True)
print('no error even though path was absent')

Making an Archive

shutil.make_archive(base_name, format, root_dir) bundles a directory into a zip or tar file in one call.

import shutil, os

os.makedirs('/tmp/arch_src', exist_ok=True)
with open('/tmp/arch_src/data.txt', 'w') as f:
    f.write('archive me')
path = shutil.make_archive('/tmp/my_archive', 'zip', '/tmp/arch_src')
print('created', os.path.basename(path))

Extracting an Archive

shutil.unpack_archive(filename, extract_dir) reverses the process, expanding a zip or tar into a folder.

import shutil, os

shutil.make_archive('/tmp/box', 'zip', '/tmp/arch_src')
shutil.unpack_archive('/tmp/box.zip', '/tmp/unpacked')
print(os.listdir('/tmp/unpacked'))

Finding Executables

shutil.which(name) locates a program on the system PATH, like the shell's which command. It returns the full path or None.

import shutil

print(shutil.which('python3'))
print(shutil.which('definitely_not_a_real_program'))

Disk Usage

shutil.disk_usage(path) reports total, used, and free bytes for the filesystem containing the path.

import shutil

usage = shutil.disk_usage('/')
print('total GB:', round(usage.total / 1e9, 1))
print('free GB:', round(usage.free / 1e9, 1))

Quick Check

Test your understanding of shutil.

Recap

You learned shutil for file operations:

  • copy/copy2 copy files; copytree copies whole trees.
  • move renames or relocates; rmtree deletes trees.
  • make_archive/unpack_archive handle zip and tar.
  • which and disk_usage query the system.

Next course: building command-line tools with argparse.

Free to start

Learn Python with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
76
Lessons
320

Frequently Asked Questions

Is the “shutil for File Operations” lesson free?

Yes — the full text of “shutil for File Operations” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Python Academy course, upgrade to CoddyKit PRO. The Python Academy course includes 4 lessons in total.

What will I learn in “shutil for File Operations”?

Copy, move, and archive files. You practise Python Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Python Academy?

No prior experience is required. Python Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.

How long does the “shutil for File Operations” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Python Academy lesson?

Yes. Every Python Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Running Commands with subprocess
  2. Capturing Output
  3. Environment and os Module
  4. shutil for File Operations
← Back to Python Academy