I am just trying to read a directory and out put the file names to a csv. The directory read works fine. It is when I try to write to the csv that I am getting this error.
line 148, in _dict_to_list wrong_fields = rowdict.keys() - self.fieldnames AttributeError: 'str' object has no attribute '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)
Your variable fields
is not a dictionary. So csv.DictWriter
will not work. How about?
writer = csv.writer(csvfile)