Fork me on GitHub

Programming Assignment - Eleventh Class

by David Pcolar

30 Sep 2013

def add_names(list_of_names, file):
  """
  Opens and adds a list of names to the end of a file, each on its own line
  """
  # We open a file in 'a' mode, for appending to it.
  names_file = open(file, 'a')

  # For each line in the list, we print that to the file.
  # This assumes one name per line.
  for name in list_of_names:
    print >> names_file, name
    print "name added: " + name

  # Close the file so the changes are visible.
  print "to file: " + file
  names_file.close()

### Main Code starts Here
new_names = input("enter new names:")

new_file = raw_input("enter the filename to append:")
add_names(new_names, new_file)

Results:

python add_names.py
enter new names:['Tim', 'Jill', 'Jeff', 'Pedro', 'Adam']
enter the filename to append:names.txt
name added: Tim
name added: Jill
name added: Jeff
name added: Pedro
name added: Adam
to file: names.txt

Dave is an IT professional with the University Library. My focus is infrastructure related project management and research on scalable storage environments. Find David Pcolar on Twitter, Github, and on the web.
comments powered by Disqus