I am attempting to pull from an API. The file runs fine if coded to run a single time. But I am having issues when coded to run once every minute (x[5]==0). The program does pull from the API (as evidenced by <print(i)>), but doesn't seem to interact with the CSV file.
import csv
import time
c = open("file.csv", "a")
w = csv.writer(c, lineterminator="\n")
while True:
  x = time.localtime()
    if x[5]==0:
      client.request(r)
      print(i)
      for i in r.response["list"]
      w.writerow([i["list_item1"], i["list_item2"]])
The program will loop through and print, but it will not write to "file.csv". I attempted to get the CSV file to close.
      client.request(r)
      print(i)
      for i in r.response["list"]
      w.writerow([i["list_item1"], i["list_item2"]])
      c.close()
With this, the program will loop through once, write to "file.csv" once, then shut down with the following error:
    w.writerow([i["list_item1"], i["list_item2"]])
ValueError: I/O operation on closed file.
I tried adding a with statement in a few places, but I couldn't get anything to work.
How can I get this to work?
 
     
    