0

I have to ask the user for an output file and then append data to it but whenever I try it tells me that the data has no append attribute. I think this is because when I try to open the file it is seeing it as a string and not an actual file to append data too. I have tried multiple ways of doing this but right now I am left with this:

Output_File = str(raw_input("Where would you like to save this data? "))
fileObject = open(Output_File, "a")
fileObject.append(Output, '\n')
fileObject.close()

The output that I am trying to append to it is just a list I earlier defined. Any help would be greatly appreciated.

1
  • If you want the list written one item per line with a newline at the end it is fileObject.write('%s\n' % '\n'.join(str(item) for item in Output)) if you want it comma separated but without the brackets it is fileObject.write('%s\n' % ', '.join(str(item) for item in Output)). Commented Oct 1, 2011 at 17:07

4 Answers 4

4

Your error is at this line:

fileObject.append(Output, '\n')

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'file' object has no attribute 'append'

Use the write method of a file object:

fileObject.write(Output+'\n')
Sign up to request clarification or add additional context in comments.

2 Comments

"The output that I am trying to append to it is just a list I earlier defined." You need to turn that list into a string, too.
@agf, yea, I saw that, but it did not jive with the posted code snippet at all. I took it to mean that they are "listing" things in a file.
2

File objects have no append method. You're looking for write. Also, str(raw_input(...)) is redundant, raw_input already returns a string.

Comments

1

The error message is pretty self-explanatory. This is because file objects don't have append method. You should simply use write:

fileObject.write(str(Output) + '\n')

1 Comment

"The output that I am trying to append to it is just a list I earlier defined." List -> string also necessary.
0
def main():
    Output = [1,2,4,4]
    Output_File = input("Where would you like to save this data?")
    fileObject = open(Output_File, 'a')
    fileObject.write(str(Output)+'\n')
    fileObject.close()
if __name__ == '__main__':
    main()

just use the .write method.

1 Comment

no need for seek, the file is already opened in append mode

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.