CertGrid CertGrid
Hands-on Lab·Python Automation for IT

Python Archives and Bulk File Operations

Copying a tree, moving files, making a backup archive and unpacking somebody else's - all of it is shutil and two archive modules, and none of it needs a subprocess. This guide also contains the most convincing demonstration of module shadowing in the whole path, because it happened to this capture: a script named copy.py broke shutil.make_archive.

Files and Data Formats Guide 16 of 39 Intermediate

tarfile extraction filters arrived in Python 3.12 and filter="data" becomes the default in 3.14 - which is why this guide passes it explicitly rather than relying on the version. zstdtar in the format list is new in 3.14.

Everything on this page runs on the control node. Any machine with Python 3 will do.
Server NameIP AddressOSRolesCPURAMHDD
RUNNER01192.168.0.27Ubuntu 26.04 LTSControl node - every script in this path runs here2 Core4 GB50 GB

This guide includes

Use this for backups and bulk file work. This matters because rmtree is the most dangerous function in the standard library, and somebody else's archive can write outside the directory you unpacked it into.

Before you start

  1. A tree to work on

  2. Copy one file, with or without its metadata

  3. Copy a whole tree

  4. Move, and rename

  5. rmtree, and the guard it deserves

  6. An archive of a directory, in one call

  7. tarfile and zipfile, when you need control

  8. Extracting somebody else's archive safely

  9. Disk space and finding a binary

  10. The accident this capture had, kept on purpose

Official sources