获取文件夹中所有路径+名称
import os
img_dir = r'/home/aistudio/data/train'
out_file = '/home/aistudio/data/train/image_paths.txt'
with open(out_file, 'w') as f:
for root, dirs, files in os.walk(img_dir):
for filename in files:
if filename.endswith('.JPG'):
file_path = os.path.join(root, filename)
print(file_path)
f.write(file_path + '\n')