[Image-SIG] Image.open should close file when it cannot identify image

Oliver Tonnhofer olt at omniscale.de
Fri Oct 2 12:06:42 CEST 2009


Hi,

I want to remove files that PIL can't open. But the following fails on  
Windows, because Image.open doesn't close the file.

----
import os
import Image
try:
     f = open('test.test', 'w')
     f.close()
     Image.open('test.test')
finally:
     os.remove('test.test')
----
results in:
WindowsError: [Error 32] The process cannot access the file because it  
is being
used by another process: 'test.test'


Below is a simple fix.

Regards,
Oliver

PS: please cc me on replies to the list.

----
--- PIL/Image.py.orig	2009-10-02 12:02:26.000000000 +0200
+++ PIL/Image.py	2009-10-02 12:03:12.000000000 +0200
@@ -1913,6 +1913,9 @@
          except (SyntaxError, IndexError, TypeError):
              pass

+    if filename:
+        fp.close()
+
      raise IOError("cannot identify image file")

  #



More information about the Image-SIG mailing list