Python-shutil 文件的复制、剪切
包子爱跑步 2019-10-29 16:49:27 1138 收藏 3
分类专栏: python 文章标签: Pythonshutil文件复制、剪切
版权
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)
————————————————
版权声明:本文为优快云博主「包子爱跑步」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.youkuaiyun.com/TYUT_xiaoming/article/details/102802687
本文介绍如何使用Python的shutil模块进行文件的复制、剪切等操作。包括将一个文件的内容复制到另一个文件、将文件复制到指定目录以及将文件从一个位置剪切到另一个位置的方法。
4万+

被折叠的 条评论
为什么被折叠?



