title: ValueError: I/O operation in closed file
body: <p>I'm getting an <code>ValueError: I/O operation in closed file</code> when running a python code. I believe this is the part that's causing the problem:  </p>
  
  <pre><code>fn = './seenFrontPagePosts.txt'
  
  try:
      f = open(fn, 'r+')
  except IOError:
      f = open(fn, 'w+')
  
  try:
      frontOld = json.loads(f.readline())
  except:
      frontOld = []
  
  for post in redditFrontPage:
      if str(post.subreddit) == subreddit:
          print("We have a post on r/all! '{}'".format(post.title))
          if str(post.id) not in frontOld:
              print("We haven't seen it before!")
              message = post.reply(allMessage)
              message.distinguish(sticky=True)
  
              frontOld.append(str(post.id))
          else:
              print("We have seen it before.")
      f.seek(0)
      f.truncate()
      f.seek(0)
      f.write(json.dumps(frontOld))
      f.close()
  </code></pre>
  
  <p>How do I fix the error? Is the syntax off or is it something more complicated?  </p>
  
  <p>Here's the full error:  </p>
  
  <pre><code>Traceback (most recent call last):
    File "pythonCode.py", line 60, in &lt;module&gt;
      f.seek(0)
  ValueError: I/O operation on closed file.
  </code></pre>