Help with capturing error

Peter Otten __peter__ at web.de
Fri Nov 28 03:58:35 EST 2008


tekion wrote:

> 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:

>>> f = open("tmp.txt", "w")
>>> f.close()
>>> f.close()
>>> f.close()
>>> f.write("yadda")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: I/O operation on closed file

Peter



More information about the Python-list mailing list