【发布时间】:2021-01-23 14:03:29
【问题描述】:
我只是想读取一个目录并将文件名输出到 csv。目录读取工作正常。当我尝试写入 csv 时,我收到了这个错误。
第 148 行,在 _dict_to_list 中 wrong_fields = rowdict.keys() - self.fieldnames AttributeError: 'str' 对象没有属性 'keys'
import os
import csv
path = 'F:\Production\FocusPoint'
filename = 'storage.csv'
fields = ['Name']
with os.scandir(path) as listOfEntries:
for entry in listOfEntries:
if entry.is_file():
f = (entry.name)
with open(filename,'w', newline='') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames= fields)
writer.writeheader()
writer.writerows(f)
【问题讨论】:
标签: python csv file output scandir