Google 网上论坛不再支持新的 Usenet 帖子或订阅项。历史内容仍可供查看。

ValueError: I/O operation on closed file

已查看 67 次
跳至第一个未读帖子

San

未读,
2016年5月25日 08:30:072016/5/25
收件人
Hi Gorup,

why i am getting "ValueError: I/O operation on closed file" this error.
Pls let me know.

Thanks in Advance.
san

Joel Goldstick

未读,
2016年5月25日 09:12:522016/5/25
收件人
On Wed, May 25, 2016 at 8:29 AM, San <sant...@gmail.com> wrote:
> Hi Gorup,
>
> why i am getting "ValueError: I/O operation on closed file" this error.
> Pls let me know.

Because your program is incorrect?

Why not list your code, so that someone might be able to help you?


>
> Thanks in Advance.
> san
> --
> https://mail.python.org/mailman/listinfo/python-list



--
Joel Goldstick
http://joelgoldstick.com/blog
http://cc-baseballstats.info/stats/birthdays

alister

未读,
2016年5月25日 10:03:372016/5/25
收件人
because you are trying to do something with a file that has been closed

the error message is quite explanatory


--
One friend in a lifetime is much; two are many; three are hardly possible.
Friendship needs a certain parallelism of life, a community of thought,
a rivalry of aim.
-- Henry Brook Adams

San

未读,
2016年5月26日 01:47:562016/5/26
收件人
Hello,
Following is the code i used.

def test_results(filename):
import csv
with open(filename,"rU") as f:
reader = csv.reader(f,delimiter="\t")
result = {}
for row in reader:
key = row[0]
if key in result:
result[row[0]].append(row[1])
else:
result[row[0]] = key
result[key]=row[1:]
print result

filename ='filename.csv'
test_results(filename)

Rustom Mody

未读,
2016年5月26日 02:17:152016/5/26
收件人
I think your for needs to be indented in the with

Steven D'Aprano

未读,
2016年5月26日 02:46:392016/5/26
收件人
On Thursday 26 May 2016 15:47, San wrote:

> Following is the code i used.
>
> def test_results(filename):
> import csv
> with open(filename,"rU") as f:
> reader = csv.reader(f,delimiter="\t")
> result = {}

You should use more consistent indents. Can you set your editor to
automatically use four spaces each time you hit the tab key?

In any case, you indent the "with" block. There are TWO lines indented, then
you outdent again. That means the with block completes and the file is closed.

You have:

def test_restults(filename):
...
with open(...) as f:
indented block
# f gets closed here
for row in reader:
indented block
print result



What you should have is:

def test_restults(filename):
...
with open(...) as f:
indented block
for row in reader:
indented block
# f gets closed here
print result


--
Steve

0 个新帖子