32

I'm working on Python and have defined a variable called "_headers" as shown below

_headers = ('id',
                'recipient_address_1',
                'recipient_address_2',
                'recipient_address_3',
                'recipient_address_4',
                'recipient_address_5',
                'recipient_address_6',
                'recipient_postcode',
                )

and in order to write this into an output file, I've written the following statement but it throws me the error "AttributeError: 'str' object has no attribute 'write'"

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

Please help

2
  • 2
    You meant : f.write(self._headers)? Commented Sep 9, 2013 at 17:17
  • Feel free to accept the answer which helped you the most. Commented Sep 9, 2013 at 17:52

4 Answers 4

31

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. :)

Sign up to request clarification or add additional context in comments.

5 Comments

Sorry for that, after the change "f.write" I now get the following error TypeError: can only concatenate tuple (not "str") to tuple
You can't write a tuple with a file object's write method. Perhaps call str or repr on it first?
@user1345260: What exactly do you want written? If you want it to appear similar to the source, use repr. If you want something to be shared with other programs, use pickle.dump or json.dump. If you want it to be formatted in some human-readable way, write the appropriate formatting code.
Or put your data into a list instead of a tuple.
You say that outfiel is a string and that file.write expects a string not a sequence Isn't that's contradicting? Could you elaborate
5

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

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

Comments

2

To stay as close to your script as possible:

>>> _headers = ('id',
...             'recipient_address_1',
...             'recipient_address_2',
...             'recipient_address_3',
...             'recipient_address_4',
...             'recipient_address_5',
...             'recipient_address_6',
...             'recipient_postcode',
...            )
>>> done = "Operation successfully completed"
>>> with open('outfile', 'w') as f:
...     for line in _headers:
...         f.write(line + "\n")
...     print done
Operation successfully completed

Comments

1

This can also do the trick. Print will "echo" to the outfile. You just need to ensure the file attribute is passed, and assign it a _io.TextIOWrapper file object. An example below: print(_headers, file=f)

#I AM USING THIS AS AN EXAMPLE:
outfile = 'out.txt'
_headers = ('id',
                    'recipient_address_1',
                    'recipient_address_2',
                    'recipient_address_3',
                    'recipient_address_4',
                    'recipient_address_5',
                    'recipient_address_6',
                    'recipient_postcode',
                    )
    
    
    with open(outfile, 'w') as f:
                print(_headers, end="", file=f)
                #print(self._headers, end="", file=f)
                print("done")

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.