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

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -