Python-shutil 文件的复制、剪切

from shutil import copyfile

from shutil import copy

from shutil import move


一个文件复制到另一个文件:copyfile

copyfile(src_path, dst_path)

将src文件内容复制至dst文件

若dst文件不存在,将会生成一个dst文件;若存在将会被覆盖

Ori_Path = '/Users/gaohuiming/Documents/Coding/jupyter/src/1.txt'
Tar_Path = '/Users/gaohuiming/Documents/Coding/jupyter/dst/2.txt'
copyfile(Ori_Path, Tar_Path)

一个文件复制到另一个文件夹:copy(src, dst)

将文件src复制至dst

dst可以是个目录,会在该目录下创建与src同名的文件

若该目录下存在同名文件,将会报错提示已经存在同名文件

from shutil import copy
import os

Ori_Path = '/Users/gao/Documents/Coding/ori_img'
Tar_Path = '/Users/gao/Documents/Coding/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)

一个文件剪切到另一个文件夹move(src, dst)

from shutil import move

src_path = '/home/A_codeTest/train/move1.jpg'
dst_path = '/home//A_codeTest/move'

move(src_path, dst_path)

reference

https://www.jb51.net/article/145522.htm

你可能感兴趣的:(python,Python,shutil,文件复制,剪切)