shutil is a module in the Python standard library for handling operations such as copying, deleting, and moving files and directories. Here are some commonly used shutil
functions:
copy copy
copy(src, dest)
: src
Copy the source file to the target file dest
.
import shutil
shutil.copy("d:/python3_test/one.txt", "d:/python3_test/two.txt")
copytree(src, dest)
: src
Copy the source directory and its subdirectories and files to the target directory dest
.
move files
move(src, dest)
src
: Move or rename the source file or directory to the target file or directory dest
.
import shutil
shutil.move("d:/python3_test/one.txt", "d:/python3_test/three.txt")
Delete folders recursively
rmtree(path)
: Recursively delete the specified directory path
and its subdirectories and files.
double naming
rename(old, new)
old
: Rename the file or directory to new
.
compression
import shutil
shutil.make_archive("archive_name", 'zip', root_dir="D:\python3_test", base_dir="shutil_test")
Link
link(src, dest)
: Create src
a hard link of the source file or directory to the destination file or directory dest.
symlink(src, dest)
: Create src
a symbolic link of the source file or directory to the target file or directory dest
.