Python uses shutil to copy files, move files, recursively delete folders, rename, compress and decompress

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 shutilfunctions:

copy copy

copy(src, dest): srcCopy 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): srcCopy 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 pathand 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 srca hard link of the source file or directory to the destination file or directory dest.
symlink(src, dest): Create srca symbolic link of the source file or directory to the target file or directory dest.

reference

https://docs.python.org/zh-cn/3/library/shutil.html

Guess you like

Origin blog.csdn.net/lilongsy/article/details/132068964