shutil for high-level file operations
shutil provides high-level file and directory operations. It is a practical tool when you need to copy, move, archive, or remove filesystem content without writing low-level loops yourself.
Why it is useful
Useful operations include:
copy()andcopy2()copytree()move()rmtree()make_archive()
Example:
import shutil
shutil.copy("report.txt", "backup/report.txt")
When to be careful
These functions can affect large parts of the filesystem quickly. Double-check paths, especially for recursive operations like rmtree() and copytree().
Rules of thumb
- Use
shutilfor high-level filesystem tasks. - Prefer built-in helpers over manual copy loops.
- Be especially careful with recursive operations.