TypeError: write() argument must be str, not dict (Python)

Viewed 7054

I am working on a project that displays my information in a txt file, it works perfectly fine with print but when I try to use write it gives me the following error:

TypeError: write() argument must be str, not dict

Here is my code:

for x in range(len(list)):
    outfile = open('message.txt', 'w')
    outfile.write(list[x])
    outfile.close()
1 Answers

You can't write a dictionary to a string. Either use print(file=file_object) or file.write(str(...)).

Code:

for x in range(len(input_list)):
    outfile = open('message.txt', 'w')
    print(input_list[x], file=outfile)
    outfile.close()
Related