[英]how to read all folder Names in directory and write folder names as header in CSV python?
I am working with images to extract data into CSV.我正在使用图像将数据提取到 CSV 中。 but currently, I want to name the header of CSV as an image folder name.
但目前,我想将 CSV 的 header 命名为图像文件夹名称。 The structure of the image directory is in this Image .
图像目录的结构在这个Image中。 The structure for my CSV file looks like as .
我的 CSV 文件的结构如下所示。 kindly help me I am a junior developer.
请帮助我,我是一名初级开发人员。
My code is我的代码是
import glob
import os
files = glob.glob('/content/drive/MyDrive/YOLO_T_ID/yolov5/runs/detect/exp5/crops/**/*.jpg', recursive=True)
for f in files:
dir_path = os.path.dirname(os.path.abspath(files))
print(f)
Why not simply using os.listdir
?为什么不简单地使用
os.listdir
?
import os
import csv
crops_dir = r"C:\Users\abokey\Desktop\crops"
img_folder_names = os.listdir(crops_dir)
with open(crops_dir+r'\output_csv.csv', 'w', newline='') as myfile:
wr = csv.writer(myfile, quoting=csv.QUOTE_ALL)
wr.writerow(img_folder_names)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.