title: Python ValueError I/O operation on closed file
body: <p>I know this sounds a common error which has been asked multiple times on stackoverflow. However, I am pretty sure my issue is new as I read through almost the related topics.</p>
  
  <p>I have two files as follows:</p>
  
  <pre><code>ALL_USER_PATH = 'all.csv'
  NEW_USER_PATH = 'new.csv'
  </code></pre>
  
  <p>I open the "all" file for reading first</p>
  
  <pre><code>with open(ALL_USER_PATH, "r") as f:
      df = pd.read_csv(f) #pd is pandas
      f.close()
  </code></pre>
  
  <p>Next, I delete the content of the "new" file and get ready to write new data to it</p>
  
  <pre><code>if os.path.isfile(NEW_USER_PATH):
      os.remove(NEW_USER_PATH)
  </code></pre>
  
  <p>Write to it and it works fine</p>
  
  <pre><code>with open(NEW_USER_PATH, "a") as csv_n:
      #writer_n is to write new users
      writer_n = csv.writer(csv_n, delimiter=",", lineterminator='\n')
      for user in customer_records:         
          if checkExistence(df): # a method I wrote before
              continue
          else:    
              writer_n.writerow([data_to_be_written])
  </code></pre>
  
  <p>Next, I delete the "all" file and write new data to it</p>
  
  <pre><code>if os.path.isfile(ALL_USER_PATH):
      os.remove(ALL_USER_PATH)
  
  with open(ALL_USER_PATH, "a") as csv_a:
      writer_a = csv.writer(csv_n, delimiter=",", lineterminator='\n')
  
      for user in customer_records:
          writer_a.writerow([all_data_to_be_written])
  </code></pre>
  
  <p>The error </p>
  
  <blockquote>
    <p>"ValueError i/o operation on closed file"</p>
  </blockquote>
  
  <p>is thrown herein at the last line to write data to the "all" file. I think it is because I opened it before, but I do remember to close it after reading the data, don't I? Could somebody please let me know what the problem is? </p>