title: Sorting dictionaries and lists from a file
body: <p>I have to write to write a program that first reads in the name of an input file and then reads the input file using the file.readlines() method. The input file contains an unsorted list of number of seasons followed by the corresponding TV show. Then my program should put the contents of the input file into a dictionary where the number of seasons are the keys, and a list of TV shows are the values (since multiple shows could have the same number of seasons).</p>
  <p>Then, I need to sort the dictionary by key (least to greatest) and output the results to a file named output_keys.txt, separating multiple TV shows associated with the same key with a semicolon (;). And finally, sort the dictionary by values (alphabetical order), and output the results to a file named output_titles.txt</p>
  <pre><code> def readFile(filename):
  
     dict = {}
  
     with open(filename, 'r') as infile:
  
         lines = infile.readlines()
  
         for index in range(0, len(lines) - 1, 2):
  
             if lines[index].strip()=='':continue
  
             count = int(lines[index].strip())
  
             show = lines[index + 1].strip()
  
             if count in dict.keys():
  
                 show_list = dict.get(count)
  
                 show_list.append(show)
  
                 show_list.sort()
  
             else:
  
                 dict[count] = [show]
  
             print(count,show)
  
     return dict
  
  def output_keys(dict, filename):
  
     with open(filename,'w+') as outfile:
  
         for key in sorted(dict.keys()):
  
             outfile.write('{}: {}\n'.format(key,'; '.join(dict.get(key))))
  
             print('{}: {}\n'.format(key,'; '.join(dict.get(key))))
  
  def output_titles(dict, filename):
  
     titles = []
  
     for title in dict.values():
  
         titles.extend(title)
  
     with open(filename,'w+') as outfile:
  
         for title in sorted(titles):
  
             outfile.write('{}\n'.format(title))
  
             print(title)
  
  def main():
  
     filename = input()
  
     dict = readFile(filename)
  
     if dict is None:
  
         print('Error: Invalid file name provided: {}'.format(filename))
  
         return
  
  
     output_filename_1 ='output_keys.txt'
  
     output_filename_2 ='output_titles.txt'
  
     output_keys(dict,output_filename_1)
  
     output_titles(dict,output_filename_2)
  
  main()
  </code></pre>
  <p>I'm not sure why this code is giving me the error below when I try to run it.
  <a href="https://i.stack.imgur.com/BHujp.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/BHujp.png" alt="enter image description here" /></a></p>
  <p>Any help is appreciated. Thank you!</p>