I am quite new to python in general and I am creating a small script that takes the input from a user and exports that data into a .txt, however I would like the exported file to be auto renamed based upon on of the inputs from the user.
firstname = raw_input("What is your First Name?" + "\n")
print 'Thank you, %s' % firstname ,
lastname = raw_input("What is your last name?" + "\n")
print 'Thank you %s' % firstname ,'%s' % lastname
age = raw_input("How old are you?" + "\n")
print 'Thank you %s' % firstname , '%s' % lastname
postcode = raw_input("What is your postal code?" + "\n")
print 'Thank you %s' % firstname , '%s' % lastname
jobtitle = raw_input("What is your current Job Title?" + "\n")
print 'Thank you %s' % firstname , '%s' % lastname
file = open("/Users/AshleyRedman/Desktop/Users [CUS] Py27/userinfo.txt", "w")
file.write(firstname +",")
file.write(lastname +",")
file.write(age +",")
file.write(postcode +",")
file.write(jobtitle)
file.close()
After this, how / what is the best way to take the 'firstname' and 'lastname' and rename the file 'user info.txt' to that?
@jonrsharpe gave you a good answer in the comments, but I thought I'd also give you some ideas since it looks like you are just trying to learn new things...
You can also do open("test.txt", "a") if you just want to open the file up to append data to it.
Also you might like to look at using the csv module. It can handle parsing csv files for you.
import csv
....
with open('userinfo.csv', 'wb') as csvfile:
csvwriter= csv.writer(csvfile, delimiter=',')
# write the csv header
csvwriter.writerow('firstname', 'lastname', 'age', 'postcode', 'jobtitle')
# write the data row (often a loop here writing rows of data)
csvwriter.writerow([firstname, lastname, age, postcode, jobtitle])