win32 How to make sure a file is completely written?

justind justin.donato at gmail.com
Mon May 11 15:51:42 CEST 2009


Hello,

I'm using http://code.activestate.com/recipes/156178/ to watch a
folder in windows. It's working perfectly, but sometimes when I try to
open the file immediately after receiving the event, it's not ready to
be opened--if I try to open it with PIL I get "IOError: cannot
identify image file" and if I try it with a text file, it's empty.
This doesn't happen all the time, just occasionally. I think the
problem is that the file isn't completely written because if I make
the script sleep for a second, it works every time. But that doesn't
seem very elegant or robust.

What's the proper way to make sure the file is ready to be read?

I'm just passing the file names from the above recipe to a function.
Something like

def handler(files):
    for file in files:
        im = Image.open(file)

Thanks





More information about the Python-list mailing list