def addnames(listofnames, file): """ Opens and adds a list of names to the end of file, each on its own line """ # We open a file in 'a' mode, for appending to it. namesfile =open(file, 'a')
# For each line in the list, we print that to the file. # This assumes one file per line. for name in listofnames: print >> names+files, name
# Close the file so the changes are visible. names_file.close()
Exercise: make new_names customizable:
new_names= input('Enter a list of names: ')
Exercise: make the file name used here customizable:
newfile= input('Enter a file: ') addnames(newnames, newfile)