My code (simplified):
import csv
def generate_record(downstream):
try:
while True:
incoming = (yield)
record = incoming.strip()
for worker in downstream:
worker.send(record)
except GeneratorExit:
for worker in downstream:
worker.close()
print('generate_record shutdown')
def file_writer(filename):
l = list()
try:
while True:
record = (yield)
l.append(record)
except GeneratorExit:
with open(filename, 'w', newline=''):
writer = csv.writer(f)
writer.writerows(l)
print('file_writer shutdown')
if __name__ == '__main__':
sink = file_writer('C:/Users/Some User/Downloads/data.csv')
next(sink)
worker = generate_record([sink])
next(worker)
with open('C:/Users/Some User/Downloads/Energy.txt') as f:
for line in f:
worker.send(line)
worker.close()
Generates the following error:
Traceback (most recent call last):
File "<ipython-input-43-ff97472f6399>", line 1, in <module>
runfile('C:/Users/Some User/Documents/Python Scripts/isii.py', wdir='C:/Users/Some User/Documents/Python Scripts')
File "C:\Users\Some User\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 699, in runfile
execfile(filename, namespace)
File "C:\Users\Some User\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 88, in execfile
exec(compile(open(filename, 'rb').read(), filename, 'exec'), namespace)
File "C:/Users/Some User/Documents/Python Scripts/isii.py", line 75, in <module>
worker.close()
File "C:/Users/Some User/Documents/Python Scripts/isii.py", line 49, in generate_record
worker.close()
File "C:/Users/Some User/Documents/Python Scripts/isii.py", line 63, in file_writer
writer.writerows(l)
ValueError: I/O operation on closed file.
What have I tried?
I've tried incrementally writing with writerow
within file_writer
within the try
block, but that generates the same error.