一、使用shutil模块的copy函数复制文件
import shutil
import os
# 创建文件
with open('my_file.txt', 'w') as f:
f.write('Hello World!')
# 将my_file.txt复制到copy_my_file.txt
shutil.copy('my_file.txt', 'copy_file_dir.txt')
二、使用shutil模块的copytree函数复制目录
import shutil
import os
# 创建多层目录
if not os.path.exists('my_directory/dir1/dir2/dir3'):
os.makedirs('my_directory/dir1/dir2/dir3')
# 创建文件
with open('my_directory/dir1/dir2/dir3/my_file.txt', 'w') as f:
f.write('世界你好')
# 将my_directory目录复制到copy_my_directory
shutil.copytree('my_directory', 'copy_my_directory')