日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

python批量處理文件或文件夾

系統 1690 0

本文實例為大家分享了python批量處理文件或文件夾的具體代碼,供大家參考,具體內容如下

            
# -*- 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)

###########批量把文件搬移到上一層文件夾并刪除當前文件夾########
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)
          

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


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 卫辉市| 大埔县| 彭阳县| 溆浦县| 绥江县| 遂溪县| 阜阳市| 灌南县| 元谋县| 张家口市| 绵阳市| 宜君县| 皮山县| 滦平县| 金山区| 葫芦岛市| 霍山县| 方正县| 唐海县| 怀柔区| 宜兰县| 岳池县| 济宁市| 永新县| 乡宁县| 库尔勒市| 陕西省| 永顺县| 广安市| 江华| 芦溪县| 西安市| 加查县| 辉南县| 九龙坡区| 江华| 昭觉县| 南和县| 定兴县| 石家庄市| 南京市|