【python】从文件夹中批量删除指定文件

耗费将近一天,主要是对python不熟悉,写的乱七八糟

最终虽然实现了,但是极度智障的程序,记录一下,等待技术见长,回看再修改吧~~~~~~~

三天后,找到了错误原因,DeleteFiles() 函数里,第三个 for 循环应该和第二个并列,如果放在第二个 for 循环里,每次取一个file 就要遍历一次 all_path_del,导致 file 里的文件是整个 all_path_del

然后重新修改了一下程序,并简化了

import glob
import os

#定义函数时,可将局部变量传入函数,否则不能运行
def DeleteFiles(path_del):
   i=0
   all_path_del = []
   for roots, dirname, files in os.walk(path_del):
       all_path_del.append(roots)
       for path in all_path_del:
           files = glob.glob(path + os.sep + '/*.mat')
           if len(files) <= 0:
               continue

        for file in files:
            i=i+1
            if os.path.basename(file)[0:7]=='Brats18':
                os.remove(file) 
                print('mat_file has been delete')
            else:
                print('mat_file  is not exists')
        print('{:d}mat_file has been delete'.format(i))                       
                                         
            
if __name__ == "__main__":    
    data_path = 'H:/brats18'
    DeleteFiles(data_path)

 

以下部分可考虑略过~~~仅留作警示~~~

import glob
import os

#generate text file of nii data 
def GenerateText(path_gen):
    all_path_gen = []
    for roots, dirname, files in os.walk(path_gen):
        all_path_gen.append(roots)
        #print(all_path)
        writeText = open('F:/mat/Nii.txt', 'w')    
        for path in all_path_gen:
            folders = glob.glob(path + os.sep + '/*.nii.gz')
            #print(folders)
            if len(folders) <= 0: #when the path is empty ,continue run the program
                continue
            writeText.write(os.sep + folders[0] + ', ' + '\n')
            writeText.write(os.sep + folders[1] + ', ' + '\n')
            writeText.write(os.sep + folders[2] + ', ' + '\n')
            writeText.write(os.sep + folders[3] + ', ' + '\n')
            writeText.write(os.sep + folders[4] + ', ' + '\n')
        writeText.close()

#delete files that name in text file named Nii 
def DeleteFiles(path_del, txt_file):
   all_path_del = []
   file_path = []
   with open('F:/mat/Nii.txt', 'r') as nii:
       for roots, dirname, files in os.walk(path_del):
           all_path_del.append(roots)
           for path in all_path_del:
               files = glob.glob(path + os.sep + '/*.nii.gz')
               if len(files) <= 0:
                   continue
               for file in files:
                    os.remove(file)
#这一块怎么都实现不了,一次只能匹配一个文件删除一个文件,气的我直接用os.remove删掉了。
#不知道txt文件生成的意义~呼~勉强看吧,继续课题了~以后再战
# =============================================================================
#                    for lines in nii:                                       
#                        if file in lines:
#                            print(file)
#                            os.remove(file)
#                    else:
#                        print(file)
#                        os.remove(file)
# =============================================================================                   
                               
if __name__ == "__main__":    
    data_path = 'F:/mat'
    txt = 'F:/mat/Nii.txt'
    #GenerateText(data_path)
    DeleteFiles(data_path, txt)

欢迎批斗~~~

 

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值