-3

I am fairly new to python, and have been trying to create a program where I create a file from user inputs. However, I cannot seem to get it to work, and keep getting the error:

Traceback (most recent call last):
    File "my_program.py", line 7, in <module>
        my_file.append(x)
AttributeError: '_io.TextIOWrapper' object has no attribute 'append'

I think this means that I cannot append the file, but I am not sure at all why. Here is the relevant part of my program:

my_file = "my_file"
with open(my_file, 'a') as my_file:
    lines = True
    counting_variable = 0
    while lines:
        x = input()
        my_file.append(x)

Thank you very much for any help in advance!

3 Answers3

0

Not able to comment yet (anyone mind converting?), but I think there are quite a few questions similar to this on SO. Try Python Append

Douglas Korinke
  • 389
  • 7
  • 20
0

Since you are already opening the file in append mode i.e open(my_file, 'a'), just write() will append your inputs.

my_file = "my_file"
with open(my_file, 'a') as my_file:
    lines = True
    counting_variable = 0
    while lines:
        x = input()
        my_file.write(x)
Bishakh Ghosh
  • 1,205
  • 9
  • 17
0

use my_file.write(x) because my_file.append() is not a valid syntax.