import os # 训练测试文件夹 def train_test_file(root,dir): file_txt = open(dir+'.txt','w') path = os.path.join(root,dir) for roots,directories,files in os.walk(path): if len(directories)!=0: dirs = directories else: now_dir = roots.split('\\') for file in files: path_1 = os.path.join(roots,file) print(path_1) file_txt.write(path_1+''+str(dirs.index(now_dir[-1]))+'\n') file_txt.close() root = r'.\食物分类\food_dataset' train_dir = 'train' test_dir = 'test' train_test_file(root,train_dir) train_test_file(foot,test_dir)
标签:创建,root,train,file,test,path,数据,dir From: https://www.cnblogs.com/Crown-V/p/17763822.html