Fork me on GitHub

Python Naming File

by Danielle Wingler

30 Sep 2013

This is what I have done so far with the code for the naming file assignment. It is functional.

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 file per line.
  for name in list_of_names:
    print >> names_file, name

  # Close the file so the changes are visible.
  names_file.close()


# Exercise: make new_names customizible:
new_names = input('Enter a list of names:')

# Exercise: make the file name used here customizible:
# This works as long as the user just types in the text file name without brackets or ', ". 
add_names(new_names, raw_input('Enter a file name:'))
Danielle is a BSIS student. Looking forward to learning more about programming. Still exploring career paths within the information science field. Find Danielle Wingler on Twitter, Github, and on the web.
comments powered by Disqus