all Python 3shutilterm
shutil
module
High-level operations on files and directory trees: copying, moving, deleting, archives.
Syntax
import shutil
os and pathlib handle one file or one directory, while shutil works on whole trees: copytree, rmtree, move, make_archive. copy carries over contents and permissions, copy2 also keeps metadata such as modification time; neither copies the owner or ACLs. rmtree deletes with no trash and no confirmation, and which looks a program up on PATH the way a shell does.
Examples
import shutil, pathlib
pathlib.Path('src').mkdir()
pathlib.Path('src/a.txt').write_text('hi')
shutil.copytree('src', 'dst')
print(pathlib.Path('dst/a.txt').read_text()) # → hi
shutil.rmtree('src')
print(pathlib.Path('src').exists()) # → False
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy