ValueError : I/O operation on closed file
up vote
59
down vote
favorite
import csv
with open('v.csv', 'w') as csvfile:
cwriter = csv.writer(csvfile, delimiter=' ', quotechar='|', quoting=csv.QUOTE_MINIMAL)
for w, c in p.iteritems():
cwriter.writerow(w + c)
Here, p
is a dictionary, w
and c
both are strings.
When I try to write in the file it reports error:
ValueError : I/O operation on closed file.
Help me, I'm really new to python. I'm working with Python 2.7.3
Thank you in advance.
python csv file-io io
add a comment |
up vote
59
down vote
favorite
import csv
with open('v.csv', 'w') as csvfile:
cwriter = csv.writer(csvfile, delimiter=' ', quotechar='|', quoting=csv.QUOTE_MINIMAL)
for w, c in p.iteritems():
cwriter.writerow(w + c)
Here, p
is a dictionary, w
and c
both are strings.
When I try to write in the file it reports error:
ValueError : I/O operation on closed file.
Help me, I'm really new to python. I'm working with Python 2.7.3
Thank you in advance.
python csv file-io io
add a comment |
up vote
59
down vote
favorite
up vote
59
down vote
favorite
import csv
with open('v.csv', 'w') as csvfile:
cwriter = csv.writer(csvfile, delimiter=' ', quotechar='|', quoting=csv.QUOTE_MINIMAL)
for w, c in p.iteritems():
cwriter.writerow(w + c)
Here, p
is a dictionary, w
and c
both are strings.
When I try to write in the file it reports error:
ValueError : I/O operation on closed file.
Help me, I'm really new to python. I'm working with Python 2.7.3
Thank you in advance.
python csv file-io io
import csv
with open('v.csv', 'w') as csvfile:
cwriter = csv.writer(csvfile, delimiter=' ', quotechar='|', quoting=csv.QUOTE_MINIMAL)
for w, c in p.iteritems():
cwriter.writerow(w + c)
Here, p
is a dictionary, w
and c
both are strings.
When I try to write in the file it reports error:
ValueError : I/O operation on closed file.
Help me, I'm really new to python. I'm working with Python 2.7.3
Thank you in advance.
python csv file-io io
python csv file-io io
edited May 23 '17 at 12:17
Ciro Santilli 新疆改造中心 六四事件 法轮功
130k27509439
130k27509439
asked Sep 23 '13 at 6:08
GobSmack
68731227
68731227
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
94
down vote
accepted
Indent correctly; for
statement should be inside with
block:
import csv
with open('v.csv', 'w') as csvfile:
cwriter = csv.writer(csvfile, delimiter=' ', quotechar='|', quoting=csv.QUOTE_MINIMAL)
for w, c in p.iteritems():
cwriter.writerow(w + c)
Outside the with
block, the file is closed.
>>> with open('/tmp/1', 'w') as f:
... print f.closed
...
False
>>> print f.closed
True
Thanks! You saved me hours of waste efforts :)
– Learner23
Jul 14 '15 at 5:38
Can't believe that this error was looked up so many times!!
– user1767754
Dec 17 '17 at 22:59
@user1767754, I faced this error: after doing a copy/paste of a code coming from Stackoverflow.com. Spaces & Tabs were mixed
– Slake
Mar 26 at 21:31
add a comment |
up vote
2
down vote
Same error can raise by mixing: tabs + spaces.
with open('/foo', 'w') as f:
(spaces OR tab) print f <-- success
(spaces AND tab) print f <-- fail
True, but this is always the case in python when mixing them up right?
– Nebulosar
Apr 20 at 9:24
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
94
down vote
accepted
Indent correctly; for
statement should be inside with
block:
import csv
with open('v.csv', 'w') as csvfile:
cwriter = csv.writer(csvfile, delimiter=' ', quotechar='|', quoting=csv.QUOTE_MINIMAL)
for w, c in p.iteritems():
cwriter.writerow(w + c)
Outside the with
block, the file is closed.
>>> with open('/tmp/1', 'w') as f:
... print f.closed
...
False
>>> print f.closed
True
Thanks! You saved me hours of waste efforts :)
– Learner23
Jul 14 '15 at 5:38
Can't believe that this error was looked up so many times!!
– user1767754
Dec 17 '17 at 22:59
@user1767754, I faced this error: after doing a copy/paste of a code coming from Stackoverflow.com. Spaces & Tabs were mixed
– Slake
Mar 26 at 21:31
add a comment |
up vote
94
down vote
accepted
Indent correctly; for
statement should be inside with
block:
import csv
with open('v.csv', 'w') as csvfile:
cwriter = csv.writer(csvfile, delimiter=' ', quotechar='|', quoting=csv.QUOTE_MINIMAL)
for w, c in p.iteritems():
cwriter.writerow(w + c)
Outside the with
block, the file is closed.
>>> with open('/tmp/1', 'w') as f:
... print f.closed
...
False
>>> print f.closed
True
Thanks! You saved me hours of waste efforts :)
– Learner23
Jul 14 '15 at 5:38
Can't believe that this error was looked up so many times!!
– user1767754
Dec 17 '17 at 22:59
@user1767754, I faced this error: after doing a copy/paste of a code coming from Stackoverflow.com. Spaces & Tabs were mixed
– Slake
Mar 26 at 21:31
add a comment |
up vote
94
down vote
accepted
up vote
94
down vote
accepted
Indent correctly; for
statement should be inside with
block:
import csv
with open('v.csv', 'w') as csvfile:
cwriter = csv.writer(csvfile, delimiter=' ', quotechar='|', quoting=csv.QUOTE_MINIMAL)
for w, c in p.iteritems():
cwriter.writerow(w + c)
Outside the with
block, the file is closed.
>>> with open('/tmp/1', 'w') as f:
... print f.closed
...
False
>>> print f.closed
True
Indent correctly; for
statement should be inside with
block:
import csv
with open('v.csv', 'w') as csvfile:
cwriter = csv.writer(csvfile, delimiter=' ', quotechar='|', quoting=csv.QUOTE_MINIMAL)
for w, c in p.iteritems():
cwriter.writerow(w + c)
Outside the with
block, the file is closed.
>>> with open('/tmp/1', 'w') as f:
... print f.closed
...
False
>>> print f.closed
True
answered Sep 23 '13 at 6:09
falsetru
240k31414416
240k31414416
Thanks! You saved me hours of waste efforts :)
– Learner23
Jul 14 '15 at 5:38
Can't believe that this error was looked up so many times!!
– user1767754
Dec 17 '17 at 22:59
@user1767754, I faced this error: after doing a copy/paste of a code coming from Stackoverflow.com. Spaces & Tabs were mixed
– Slake
Mar 26 at 21:31
add a comment |
Thanks! You saved me hours of waste efforts :)
– Learner23
Jul 14 '15 at 5:38
Can't believe that this error was looked up so many times!!
– user1767754
Dec 17 '17 at 22:59
@user1767754, I faced this error: after doing a copy/paste of a code coming from Stackoverflow.com. Spaces & Tabs were mixed
– Slake
Mar 26 at 21:31
Thanks! You saved me hours of waste efforts :)
– Learner23
Jul 14 '15 at 5:38
Thanks! You saved me hours of waste efforts :)
– Learner23
Jul 14 '15 at 5:38
Can't believe that this error was looked up so many times!!
– user1767754
Dec 17 '17 at 22:59
Can't believe that this error was looked up so many times!!
– user1767754
Dec 17 '17 at 22:59
@user1767754, I faced this error: after doing a copy/paste of a code coming from Stackoverflow.com. Spaces & Tabs were mixed
– Slake
Mar 26 at 21:31
@user1767754, I faced this error: after doing a copy/paste of a code coming from Stackoverflow.com. Spaces & Tabs were mixed
– Slake
Mar 26 at 21:31
add a comment |
up vote
2
down vote
Same error can raise by mixing: tabs + spaces.
with open('/foo', 'w') as f:
(spaces OR tab) print f <-- success
(spaces AND tab) print f <-- fail
True, but this is always the case in python when mixing them up right?
– Nebulosar
Apr 20 at 9:24
add a comment |
up vote
2
down vote
Same error can raise by mixing: tabs + spaces.
with open('/foo', 'w') as f:
(spaces OR tab) print f <-- success
(spaces AND tab) print f <-- fail
True, but this is always the case in python when mixing them up right?
– Nebulosar
Apr 20 at 9:24
add a comment |
up vote
2
down vote
up vote
2
down vote
Same error can raise by mixing: tabs + spaces.
with open('/foo', 'w') as f:
(spaces OR tab) print f <-- success
(spaces AND tab) print f <-- fail
Same error can raise by mixing: tabs + spaces.
with open('/foo', 'w') as f:
(spaces OR tab) print f <-- success
(spaces AND tab) print f <-- fail
answered Mar 26 at 21:43
Slake
1,29621724
1,29621724
True, but this is always the case in python when mixing them up right?
– Nebulosar
Apr 20 at 9:24
add a comment |
True, but this is always the case in python when mixing them up right?
– Nebulosar
Apr 20 at 9:24
True, but this is always the case in python when mixing them up right?
– Nebulosar
Apr 20 at 9:24
True, but this is always the case in python when mixing them up right?
– Nebulosar
Apr 20 at 9:24
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f18952716%2fvalueerror-i-o-operation-on-closed-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown