-1

Trying to write a dictionary to a raw text file:

doc = {'foo':'bar'}
f = '/home/pi/Documents/samplefile.txt'
backup__file = open(f, 'a')
f.write(str(doc))
f.close

but getting the error str has no attribute write. How can I just dump a dictionary into a blank text file?

iFunction
  • 1,208
  • 5
  • 21
  • 35

2 Answers2

0

You're calling the write function on the path not on the file. Try calling it on your backup__file variable. Have a look at following example. Python: Writing a dictionary to a csv file with one line for every 'key: value'

Community
  • 1
  • 1
Stijn Van Daele
  • 285
  • 1
  • 14
0

My bad, f is the file path, backup_file is the actual file, it should be:

doc = {'foo':'bar'}
f = '/home/pi/Documents/samplefile.txt'
backup__file = open(f, 'a')
backup_file.write(str(doc))
backup_file.close
iFunction
  • 1,208
  • 5
  • 21
  • 35