简体   繁体   English

如何读取目录中的所有文件夹名称并将文件夹名称写入 CSV python 中的 header?

[英]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)

# Output: # Output:

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 目录中的 Python 文件夹名称 - Python folder names in the directory Python - 为文件夹中的所有 csv 文件制作新的 csv 文件,名称为 header 以及它们来自的文件 - Python - for all csv files in folder make new csv file with header names and the file they came from 从文件夹路径读取 CSV 的标题,并将标题行写入另一个 csv,其结构为表名和列名 - Read title of CSV's from a folder path , and write the header rows to another csv with structure as Table Name and Column Names Python - 将根目录名称与所有子文件夹名称连接起来 - Python - concatenate root directory name with all sub-folder names 如何合并文件夹中的所有 csv 文件,但它们都有不同的名称 - How to merge all csv files in a folder, but they all have different names 如何在python中更改文件夹名称? - How to change folder names in python? Python - 仅列出文件夹名称而不是完整目录 - Python - Only listing folder names and not full directory 如何从目录中的文件夹名称中提取后缀字符串? (蟒蛇) - How to Extract A String of the Suffix from Folder Names in a Directory? (Python) 如何从基于 python 文件夹名称列表的目录中获取 select 文件夹? - How to select folders from a directory based on a python list of the folder names? 使用 python,如何读取文件夹中的所有 CSV 文件并将其内容写入新的单个 CSV 文件? - Using python, how to read all the CSV files in a folder and write the content of the same to a new single CSV file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM