国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Python > 正文

python批量復(fù)制圖片到另一個(gè)文件夾

2020-02-15 23:00:07
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文實(shí)例為大家分享了python批量復(fù)制圖片到文件夾的具體代碼,供大家參考,具體內(nèi)容如下

直接上代碼:

# -*- coding: utf-8 -*-"""Created on Mon Apr 02 21:03:44 2018@author: Fsl"""import shutil#這個(gè)庫(kù)復(fù)制文件比較省事 def objFileName(): ''' 生成文件名列表 :return: ''' local_file_name_list = r'G:/KeTi/OCT/ImageSets/Main/test.txt' #指定名單 obj_name_list = [] for i in open(local_file_name_list,'r'):  obj_name_list.append(i.replace('/n','')) return obj_name_list def copy_img(): ''' 復(fù)制、重命名、粘貼文件 :return: ''' local_img_name=r'G:/KeTi/OCT/JPEGImages' #指定要復(fù)制的圖片路徑 path = r'G:/KeTi/OCT/data' #指定存放圖片的目錄 for i in objFileName():  new_obj_name = i+'.jpg'  shutil.copy(local_img_name+'/'+new_obj_name,path+'/'+new_obj_name) if __name__ == '__main__': copy_img()

就這么多,很簡(jiǎn)單。

小編再為大家分享python實(shí)現(xiàn)圖片批量復(fù)制或刪除的代碼,如下

#coding=utf-8import osimport shutil  #遞歸復(fù)制文件夾內(nèi)的文件def copyFiles(sourceDir,targetDir):   #忽略某些特定的子文件夾  if sourceDir.find("exceptionfolder")>0:     return    #列出源目錄文件和文件夾  for file in os.listdir(sourceDir):     #拼接完整路徑    sourceFile = os.path.join(sourceDir,file)     targetFile = os.path.join(targetDir,file)      #如果是文件則處理    if os.path.isfile(sourceFile):       #如果目的路徑不存在該文件就創(chuàng)建空文件,并保持目錄層級(jí)結(jié)構(gòu)      if not os.path.exists(targetDir):         os.makedirs(targetDir)       #如果目的路徑里面不存在某個(gè)文件或者存在那個(gè)同名文件但是文件有殘缺,則復(fù)制,否則跳過(guò)      if not os.path.exists(targetFile) or (os.path.exists(targetFile) and (os.path.getsize(targetFile) != os.path.getsize(sourceFile))):        open(targetFile, "wb").write(open(sourceFile, "rb").read())         print targetFile+" copy succeeded"     #如果是文件夾則遞歸    if os.path.isdir(sourceFile):       copyFiles(sourceFile, targetFile) #遍歷某個(gè)目錄及其子目錄下所有文件拷貝到某個(gè)目錄中def copyFiles2(srcPath,dstPath):   if not os.path.exists(srcPath):     print "src path not exist!"   if not os.path.exists(dstPath):     os.makedirs(dstPath)   #遞歸遍歷文件夾下的文件,用os.walk函數(shù)返回一個(gè)三元組  for root,dirs,files in os.walk(srcPath):     for eachfile in files:       shutil.copy(os.path.join(root,eachfile),dstPath)       print eachfile+" copy succeeded"  #刪除某目錄下特定文件def removeFileInDir(sourceDir):  for file in os.listdir(sourceDir):    file=os.path.join(sourceDir,file) #必須拼接完整文件名    if os.path.isfile(file) and file.find(".jpg")>0:      os.remove(file)      print file+" remove succeeded" if __name__ =="__main__":   copyFiles("./dir1","./dir2")  #removeFileInDir("./dir2")  #copyFiles2("./dir1","./dir2")            
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 通江县| 手游| 偏关县| 赞皇县| 英吉沙县| 曲沃县| 淮北市| 绥棱县| 三原县| 建阳市| 香港 | 新干县| 涟源市| 平度市| 海安县| 平原县| 衡阳市| 垫江县| 永胜县| 兴业县| 宾阳县| 连云港市| 绥棱县| 阜新市| 札达县| 德保县| 且末县| 威海市| 同仁县| 泰安市| 北碚区| 碌曲县| 甘南县| 夏津县| 新密市| 大厂| 黔南| 卓资县| 山丹县| 龙南县| 萝北县|