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)
————————————————
版權聲明:本文為CSDN博主「包子愛跑步」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/TYUT_xiaoming/article/details/102802687
