How to check whether file is open or not

Diez B. Roggisch deets at nospam.web.de
Wed Mar 7 04:11:18 EST 2007


Ros wrote:

> There are 10 files in the folder. I wish to process all the files one
> by one. But if the files are open or some processing is going on them
> then I do not want to disturb that process. In that case I would
> ignore processing that particular file and move to next file.
> 
> How can I check whether the file is open or not?
> 
> I tried os.stat and os.access but I am not getting the expected
> results.
> Also I checked in IO exceptions, IO error handler doesnt raise any
> error if the file is open.
> There are some options to check but they are platform dependent (works
> only with unix)

You can't do that, at least not in a platform agnostic way, and usually not
without cooperation of the file-modifying processes.

Diez



More information about the Python-list mailing list