shutil模块
from shutil import copyfile
from shutil import copy
from shutil import move
① 文件复制
copyfile(src_path, dst_path) 将src文件内容复制至dst文件, 若dst文件不存在,将会生成一个dst文件,若存在将会被覆盖
Ori_Path = 'e:/ori/1.txt'
Tar_Path = 'e:/dst/2.txt'
copyfile(Ori_Path, Tar_Path)
② 文件复制到文件夹内
copy(src_path, dst_path) 将src文件内容复制至dst文件夹内, 若存在同名的文件,则会报错
from shutil import copy
import os
Ori_Path = 'e:/assets/ori_img'
Tar_Path = 'e:/assets/album/copy_img'
img_names = os.listdir(Ori_Path)
for img_name in img_names:
ori_img_path = Ori_Path + '/' + img_name
copy(ori_img_path, Tar_Path)
③ 剪切文件到文件夹内
from shutil import copy
import os
Ori_Path = 'e:/assets/ori_img'
Tar_Path = 'e:/assets/album/copy_img'
img_names = os.listdir(Ori_Path)
for img_name in img_names:
ori_img_path = Ori_Path + '/' + img_name
move(ori_img_path, Tar_Path)