亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

Python文件夾批處理操作代碼實例

瀏覽:69日期:2022-07-16 16:55:30

如圖所示,有一個test文件夾,里面有3個子文件夾,每個子文件夾中有若干圖片文件

Python文件夾批處理操作代碼實例

Python文件夾批處理操作代碼實例

#場景1 讀取一個文件夾中所有文件,存入到一個list表中

Python文件夾批處理操作代碼實例

#coding:utf-8from __future__ import absolute_importfrom __future__ import divisionfrom __future__ import print_functionimport numpy as npimport osimport sysimport mathimport numpyimport timeimport argparseimport randomimport cv2def findAllfile(path, allfile): filelist = os.listdir(path) for filename in filelist: filepath = os.path.join(path, filename) if os.path.isdir(filepath): #print(filepath) findAllfile(filepath, allfile) else: allfile.append(filepath) return allfile #場景1 讀取一個文件夾中所有文件,存入到一個list表中def process1(srcpath, imgprocess_result): #遍歷圖像文件夾 image_files = findAllfile(srcpath,[]) #image_files為所有文件的list #判斷 目錄是否存在,存在就刪除,并且重建 if os.path.exists(imgprocess_result): os.system('rm -rf ' + imgprocess_result) if not os.path.isdir(imgprocess_result): # Create the log directory if it doesn’t exist os.makedirs(imgprocess_result) #是否隨機打亂文件順序 #random.shuffle(image_files) #遍歷所有 for facepath in image_files: print('原始文件路徑:', facepath) #獲取文件名稱 data_split = facepath.strip().split('/') image_floder = data_split[-2] print('文件所在目錄:', image_floder) image_name = data_split[-1] print('文件名稱:', image_name) image_newfloder = imgprocess_result + '/' + image_floder #判斷 目錄是否存在,不存在就重建 if not os.path.isdir(image_newfloder): # Create the log directory if it doesn’t exist os.makedirs(image_newfloder) image_newpath = image_newfloder + '/' + image_name print('處理后的文件路徑:', image_newpath) #開始處理文件 #.............. # ##場景2 首先讀取一個文件夾中的所有子目錄,然后依次遍歷各個子目錄的所有文件def process2(srcpath, imgprocess_result): #找出所有的子目錄 filelist = os.listdir(srcpath) for filename in filelist: filepath = os.path.join(srcpath, filename) if os.path.isdir(filepath): print('原始子目錄路徑:', filepath) image_files = findAllfile(filepath,[]) for facepath in image_files:print('原始文件路徑:', facepath)#獲取文件名稱data_split = facepath.strip().split('/')image_floder = data_split[-2]print('文件所在目錄:', image_floder)image_name = data_split[-1]print('文件名稱:', image_name)image_newfloder = imgprocess_result + '/' + image_floder#判斷 目錄是否存在,不存在就重建if not os.path.isdir(image_newfloder): # Create the log directory if it doesn’t exist os.makedirs(image_newfloder)image_newpath = image_newfloder + '/' + image_nameprint('處理后的文件路徑:', image_newpath)#開始處理文件#..............##if __name__ == ’__main__’: #原始文件夾 srcpath = '/DATA/share/publicdata/face/dmt_face/test' #處理完畢后存放文件 imgprocess_result = '/DATA/share/publicdata/face/dmt_face/imgprocess_result' print('方法1nnnn') process1(srcpath, imgprocess_result) print('nnn方法2') process2(srcpath, imgprocess_result)

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 中文福利视频 | 成人性生交大片免费看4 | 五月婷婷综合激情网 | 九九热8 | 在线看黄免费 | 一级特黄特黄xxx视频 | 在线精品日韩一区二区三区 | 一级做a爰片久久毛片16 | 久久国产免费观看精品3 | 在线精品国精品国产不卡 | 亚洲欧美日韩一区二区 | 国产亚洲欧洲日韩综合v | 伊人久久在线观看 | 国产精品一区二区三区免费 | 一级黄色淫片 | 国产区精品福利在线观看精品 | 亚洲免费中字慕日产2021 | 日韩日b| 特黄十八岁大片 | 日韩在线 在线播放 | 在线观看一区二区精品视频 | 日本精品久久久久中文字幕2 | 国产视频网站在线 | 黄色大片网 | 精品福利一区二区三区 | 96免费精品视频在线观看 | 亚洲视频综合网 | 成人免费网址 | xxxxx大片在线观看 | 在线观看网 | 国产亚洲第一伦理第一区 | 国产欧美日韩综合精品一区二区 | 精品九九久久国内精品 | 爱操综合 | 国内精品福利爱丝视频_速发 | 久久精品国产只有精品2020 | 国产亚洲女人久久久久久 | 欧美极品妇xxxxxbbbbb | 91精品啪在线看国产网站 | 精品国产三级在线观看 | 国产精品合集久久久久青苹果 |