Reputation: 157
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.
Upvotes: 0
Views: 974
Reputation: 3435
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.
Upvotes: 0
Reputation: 24429
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')
Upvotes: 1
Reputation: 129764
File objects have no append
method. You're looking for write
. Also, str(raw_input(...))
is redundant, raw_input
already returns a string.
Upvotes: 2
Reputation: 108512
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')
Upvotes: 4