0Pricing
Python Academy · Lesson

shutil for File Operations

Copy, move, and archive files.

shutil for File Operations is a free Python Academy lesson on CoddyKit — 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, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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.

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, and the Python Academy course includes 4 lessons in total. 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.

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; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

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