title: Error: 'Bad file descriptor'
body: <p>I don't understand why I keep getting the following error message when I run my code:</p>
  
  <pre><code>with open(classChoice, 'r+') as resultfile:
  OSError: [Errno 9] Bad file descriptor
  </code></pre>
  
  <p>This is my code:</p>
  
  <pre><code>import collections
  
  classChoice = int(input("Which class do you want to order? "))
  if classChoice == 1:
      open ("Class 1.txt")
  elif classChoice == 2:
      open ("Class 2.txt")
  else:
      open ("Class 3.txt")
  
  scores = {}
  def alphabetical():
      with open(classChoice, 'r+') as resultfile: 
          for line in resultfile:
              name, score = lines.split(":")
              scores.setdefault(name, collections.deque(maxlen=3)).append(int(score))
  
      for name in sorted(scores):
          m = max(scores[name])
          print('{name}: {m}'.format(name=name, m=m))
  
  def highestScore():
      with open(classChoice, 'r+') as resultfile:
          for line in resultfile:
              name, score = line.split(":")
              scores.setdefault(name, collections.deque(maxlen=3)).append(int(score))
  
      for name in sorted(scores, key=lambda name: max(scores[name]), reverse=True):
          m = max(scores[name])
          print('{name}: {m}'.format(name=name, m=m))
  
  def averageScore():
      with open(classChoice, 'r+') as resultfile:
          d = {}
          for line in resultfile:
              column = line.split(":")
              names = column[0]
              scores = int(column[1].strip())
              d.setdefault(names, []).append(scores)
          averages=[]
  
          for names, v in d.items():
              average = (sum(v[-3:])/len(v[-3:]))
              averages.append((names, average))
          for names, average in sorted(averages, key=lambda a: a[1], reverse=True):
              print(names, average)
  
  orderChoice = (input("How do you want to order the data? "))
  if orderChoice == "alphabetically":
                    alphabetical()
  elif orderChoice == "with highest score for the tests, highest to lowest":
                    highestScore()
  else:
                    averageScore()              
  </code></pre>