. Advertisement .
..3..
. Advertisement .
..4..
I don’t know what I’m doing wrong, but I’ve already lost a couple of days struggling with this. Here is my command line:
import csv
with open('v.csv', 'w') as csvfile:
cwriter = csv.writer(csvfile, delimiter=' ', quotechar='|', quoting=csv.QUOTE_MINIMAL)
for w, c in p.items():
cwriter.writerow(w + c)
This returns:
ValueError: I/O operation on closed file.
I don’t have any experience with valueerror: i/o operation on closed file.. In this case, how should I change?
This type of “ValueError: I/O operation on closed file” happens in two cases as follows:
Case 1: When you try to write on a file after it has been closed using the close() statement. Here is the example:
In the code above, when we try to write another statement into a file named “v.csv” after closing it, ValueError appears.
Case 2:
In the code above, we try to open and write something on a file named “v.csv”, and then close it. However, the “One more line” statement is used outside the with block. Therefore, the ValueError occurs because of incorrect indentation.
Your issue is belong to Case 2, here is solution for it:
Your “One more line” statement must be within the block.
The same error can be raised by Mixing: Tabs + Spaces.
Your
for
statement must be within the blockThe file is now closed except for the
with
block.