[Image-SIG] Re: Image.open doesn't close file before raising IOError "cannot identify image file"
Fredrik Lundh
fredrik@pythonware.com
Sun, 13 Apr 2003 14:27:20 +0200
"aut imagesig" <imagesig@aut.mailshell.com> wrote:
> try:
> im = Image.open(filename)
> except IOError:
> os.remove(filename)
that doesn't work if the file doesn't exist.
a more robust solution (which doesn't require any changes to
the library) is to open the file at the application level:
try:
file = open(filename, "rb")
except IOError:
# the file doesn't appear to exist
...
else:
try:
im = Image.open(file)
except IOError:
# not an image file; get rid of it
file.close()
os.remove(filename)
</F>