close

[Solved] AttributeError: ‘str’ object has no attribute ‘write’

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error AttributeError: ‘str’ object has no attribute ‘write’ in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How AttributeError: ‘str’ object has no attribute ‘write’ Error Occurs?

Today I get the following error AttributeError: ‘str’ object has no attribute ‘write’ in python.

How To Solve AttributeError: ‘str’ object has no attribute ‘write’ Error ?

  1. How To Solve AttributeError: 'str' object has no attribute 'write' Error ?

    To Solve AttributeError: 'str' object has no attribute 'write' Error As noted in the comments, file.write expects a string, not a sequence.

  2. AttributeError: 'str' object has no attribute 'write'

    To Solve AttributeError: 'str' object has no attribute 'write' Error As noted in the comments, file.write expects a string, not a sequence.

Solution 1

You want f.write, not outfile.write

outfile is the name of the file as a string. f is the file object.

As noted in the comments, file.write expects a string, not a sequence. If you wanted to write data from a sequence, you could use file.writelines. e.g. f.writelines(self._headers). But beware, this doesn’t append a newline to each line. You need to do that yourself. 🙂

Solution 2

Assuming that you want 1 header per line, try this:

with open(outfile, 'w') as f:
    f.write('\n'.join(self._headers))  
    print done

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read