title: How do I alphabetize a file in Python?
body: <p>I am trying to get a list of presidents alphabetized by last name, even though the file that it is being drawn is currently listed first name, last name, date in office, and date out of office.</p>
  
  <p>Here is what I have, any help on what I need to do with this. I have searched around for some answers, and most of them are beyond my level of understanding. I feel like I am missing something small. I tried to break them all out into a list, and then sort them, but I could not get it to work, so this is where I started from. </p>
  
  <pre><code>INPUT_FILE = 'presidents.txt'
  OUTPUT_FILE = 'president_NEW.txt'
  OUTPUT_FILE2 = 'president_NEW2.txt'
  
  def main():
    infile = open(INPUT_FILE)
    outfile = open(OUTPUT_FILE, 'w')
    outfile2 = open(OUTPUT_FILE2,'w')
  
    stuff = infile.readline()
  
    while stuff:
      stuff = stuff.rstrip()
      data = stuff.split('\t')
  
      president_First = data[1]
      president_Last = data[0]
      start_date = data[2]
      end_date = data[3]
  
      sentence = '%s %s was president from %s to %s' % \
                (president_First,president_Last,start_date,end_date)
      sentence2 = '%s %s was president from %s to %s' % \
                 (president_Last,president_First,start_date, end_date)
  
      outfile2.write(sentence2+ '\n')
      outfile.write(sentence + '\n')
  
      stuff = infile.readline()
  
    infile.close()
    outfile.close()
  
  main()
  </code></pre>