本篇文章給大家分享的是有關(guān)python中怎么批量處理文件,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
網(wǎng)站制作、建網(wǎng)站找專業(yè)網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián):定制網(wǎng)站、模板網(wǎng)站、仿站、微信小程序開發(fā)、軟件開發(fā)、成都app開發(fā)等。做網(wǎng)站價(jià)格咨詢創(chuàng)新互聯(lián):服務(wù)完善、十年建站、值得信賴!網(wǎng)站制作電話:028-86922220具體內(nèi)容如下
# -*- coding: utf-8 -*- import os,shutil import sys import numpy as np ##########批量刪除不同文件夾下的同名文件夾############# def arrange_file(dir_path0): for dirpath,dirnames,filenames in os.walk(dir_path0): if 'my_result' in dirpath: # print(dirpath) shutil.rmtree(dirpath) ##########批量在不同文件夾下新建同名子文件夾并把文件搬移到子文件夾############# def arrange_file(dir_path0): for dirpath,dirnames,filenames in os.walk(dir_path0): for files in filenames: total_path = os.path.join(dirpath,files) root_path,file_path = total_path.split(dir_path,1) if 'png' in file_path: new_file_path = '.' + file_path[:-9] + 'new_file_name/' # print(file_path) # print(new_file_path) # print(new_file_path + file_path[-9:]) # if not os.path.exists(new_file_path): # os.makedirs(new_file_path) # shutil.move('.' + file_path,new_file_path + file_path[-9:]) ##########批量刪除不同文件夾下符合條件的文件################## def arrange_file(dir_path0): for dirpath,dirnames,filenames in os.walk(dir_path0): for files in filenames: total_path = os.path.join(dirpath,files) # print(total_path) if 'jpg' in total_path and 'labels' in total_path: img = cv2.imread(total_path) if np.sum(img) == 0: print(total_path) os.remove(total_path) ###########批量把文件搬移到上一層文件夾并刪除當(dāng)前文件夾######## def arrange_file(dir_path0): for dirpath,dirnames,filenames in os.walk(dir_path0): for files in filenames: total_path = os.path.join(dirpath,files) root_path,file_path = total_path.split(dir_path0,1) # print(file_path[:-48]) # return 0 if 'jpg' in file_path: new_file_path = dir_path0 + file_path[:-48] shutil.move(dir_path0 + file_path,new_file_path + file_path[-9:]) for dirpath,dirnames,filenames in os.walk(dir_path0): file_path = dirpath.split('./your_total_path')[1] if 'keywords' in file_path: # print(dirpath) shutil.rmtree(dirpath) if __name__=='__main__': dir_path0 = './your_total_path' arrange_file(dir_path0)
以上就是python中怎么批量處理文件,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過這篇文章學(xué)到更多知識(shí)。更多詳情敬請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。