耗费将近一天,主要是对python不熟悉,写的乱七八糟。
最终虽然实现了,但是极度智障的程序,记录一下,等待技术见长,回看再修改吧~~~~~~~
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)