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
- Control nodeUbuntu 26.04 LTS
- Python3.14.4
- requests2.34.2
- paramiko5.0.0
- TimeAbout 18 min
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.
- pytest9.1.1
- PyYAML6.0.3
- boto3 / botocore1.43.78
| Server Name | IP Address | OS | Roles | CPU | RAM | HDD |
|---|---|---|---|---|---|---|
| RUNNER01 | 192.168.0.27 | Ubuntu 26.04 LTS | Control node - every script in this path runs here | 2 Core | 4 GB | 50 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.
- copying a file with or without its metadata, and copying a whole tree
- moving and renaming, including across filesystems
- using
rmtreewith the guard it deserves - making and unpacking an archive, then dropping to
tarfileandzipfilefor control - extracting somebody else's archive safely, and reading the accident this capture kept on purpose
Before you start
-
A tree to work on
-
Copy one file, with or without its metadata
-
Copy a whole tree
-
Move, and rename
-
rmtree, and the guard it deserves
-
An archive of a directory, in one call
-
tarfile and zipfile, when you need control
-
Extracting somebody else's archive safely
-
Disk space and finding a binary
-
The accident this capture had, kept on purpose