How to write data to a text file in python? -
this have far:
def main(): infolist = [] count = 0 while true: firstname = input('please enter first name: ') mystring = str(firstname) lastname = input('please enter last name: ') mystring2 = str(lastname) telephoneno = input('please enter telephone number: ') mystring3 = str(telephoneno) contiinue = input('continue (y = yes): ') if contiinue == 'y': count = count + 1 else: print ("file written") break file = open('filename', 'a'); file.write(data.to_string()); file.close(); main()
i'm trying program write input text file, allow new information added added text file, not erase whats been written.
every time try run program there's problem main() , name error, data not defined?
to add @clodion's answer use with
keyword
def main(): infolist = [] count = 0 while true: fnane = input('please enter first name: ') lname = input('please enter last name: ') tele = input('please enter telephone number: ') ok = input('continue (y = yes): ') if ok == 'y': count = count + 1 else: print ("file written") break data = fname + lname + tele open('filename', 'a') file: file.write(data); main()
Comments
Post a Comment