带文件路径
import os
path = ['/home/yuxin/Mask_RCNN-master-Abyssinian/datasets/Abyssinian']
f = open('dataset1.txt','w')
for path in path:
p = os.path.abspath(path)+'/Abyssinian_pic'
filenames = os.listdir(p)
for filename in filenames:
im_path = p + '/'+filename
print(im_path)
f.write(im_path + '\n')
f.close()
结果图
不带文件路径
import os
f= open('/home/yuxin/Mask_RCNN-master-Abyssinian/datasets/Abyssinian/dataset.txt','w')
for root ,dirs,files in os.walk('/home/yuxin/Mask_RCNN-master-Abyssinian/datasets/Abyssinian/Abyssinian_pic',True):
for file in files:
f.writelines(file)
f.write('\n')
打开dataset.txt文件如下。但是在win下直接打开txt是没有换行的