Help with capturing error

T

tekion

Hello,
I am getting the following error and my script is bailing out because
of it. I have tried capturing it but it does not seem to work. Below
is the error:

ValueError: I/O operation on closed file


the above error is received when, the following code snippet is
executed:
try:
117 self.xml_file.close()
118 except ValueError:
119 print "file, %s is already closed" % self.seek_file

Any idea why line 118, is not capturing the error? Thanks.
 
D

Diez B. Roggisch

tekion said:
Hello,
I am getting the following error and my script is bailing out because
of it. I have tried capturing it but it does not seem to work. Below
is the error:

ValueError: I/O operation on closed file


the above error is received when, the following code snippet is
executed:
try:
117 self.xml_file.close()
118 except ValueError:
119 print "file, %s is already closed" % self.seek_file

Any idea why line 118, is not capturing the error? Thanks.

This is not valid python - it obviously isn't indented properly.

The following piece of code works flawlessly for me:



outf = open("/tmp/test.dat", "w")
outf.write("foo")
outf.close()
try:
outf.close()
except ValueError:
pass



Diez
 
C

Chris Rebert

Hello,
I am getting the following error and my script is bailing out because
of it. I have tried capturing it but it does not seem to work. Below
is the error:

ValueError: I/O operation on closed file


the above error is received when, the following code snippet is
executed:
try:
117 self.xml_file.close()
118 except ValueError:
119 print "file, %s is already closed" % self.seek_file

Any idea why line 118, is not capturing the error? Thanks.

In order to help you debug an exception, generally a full Traceback is
necessary.
If you post the full traceback, it will help people immensely in
helping determine the cause of your problem.

Cheers,
Chris[/QUOTE]
 
P

Peter Otten

tekion said:
Hello,
I am getting the following error and my script is bailing out because
of it. I have tried capturing it but it does not seem to work. Below
is the error:

ValueError: I/O operation on closed file


the above error is received when, the following code snippet is
executed:
try:
117 self.xml_file.close()
118 except ValueError:
119 print "file, %s is already closed" % self.seek_file

Any idea why line 118, is not capturing the error? Thanks.

It seems you didn't read the traceback carefully. Line 117 doesn't cause
that error. You can close a file as often as you like, but you can't
read/write to a closed file:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: I/O operation on closed file

Peter
 
T

tekion

Yes, you are absolutely right. I had open a file earlier and when it
reach end of line with no new idea; it seems to have closed the file.
I am not sure if this because I am using my own class to open and read
a file or just a python behavior. I plan to test this out.

On another note, is there a way for python to emulate a "tail -f" in
Linux? Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top