This is my code:
import csv
with open('test.csv', 'rb') as csvfile:
x = csv.reader(csvfile,delimiter=',',quotechar='|')
for row in x:
print (row)
I get the following error:
ValueError: readline of closed file
What might be the problem?
Your indent is out of order
import csv
with open('test.csv', 'rb') as csvfile:
x = csv.reader(csvfile,delimiter=',',quotechar='|')
for row in x:
print (row)
Is the correct indent
The line, with open('test.csv', 'rb') as csvfile:
creates a file object, but calls the __close__
method of the file once its block ends. As in Python, an un-indent closes the block, you have exited the block when you write for row in x:
. Thus x
is now closed, and you cannot perform any operations on that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With