- Python 中的
shutil
库提供了复制、移动和归档文件和目录的方法。使用shutil
库的方法如下:- 导入库:
import shutil
- 复制文件:
shutil.copy(src, dst)
,指定源文件src
和新文件的位置dst
。 - 移动文件:
shutil.move(src, dst)
,指定源文件src
和新文件的位置dst
。 - 归档文件夹:
shutil.make_archive(base_name, format, root_dir)
,指定归档文件的名称base_name
、格式format
(如:“zip”)和要归档的根目录root_dir
。
- 导入库:
示例:
import shutil
# 复制文件
shutil.copy('example_file.txt', 'example_file_copy.txt'