How to find out if another process is using a file

Jean-Paul Calderone exarkun at divmod.com
Thu Jan 18 12:46:59 EST 2007


On Thu, 18 Jan 2007 14:34:52 -0300, Gabriel Genellina <gagsl-py at yahoo.com.ar> wrote:
>"Tom Wright" <tew24 at spam.ac.uk> escribió en el mensaje
>news:eoo6vd$33u$1 at gemini.csx.cam.ac.uk...
>
>> I'm writing a program which reads a series of data files as they are
>> dumped
>> into a directory by another process.  At the moment, it gets sporadic bugs
>> when it tries to read files which are only partially written.
>>
>> I'm looking for a function which will tell me if a file is opened in
>> write-mode by another process - if it is, my program will ignore it for
>> now
>> and come back to it later.  This needs to work on linux and windows.  Mac
>> OS would be a bonus too.  An os-independent solution would be good, but I
>> could write os-specific options and have it pick the appropriate one.
>
>Use os.open with the O_EXCL flag; will fail if the other process has the
>file still open (and will fail if another process is reading the file, too,
>not just if someone is writing).

On what platform is this true?

A better solution is to name or place files which are begin written in a which is recognizable and only rename or move them to their final location when they have been completely written.

For example, name files ".new" as they are being written.  When they are fully written, drop the ".new" suffix.  On the reader side, ignore any file with a name ending in ".new".

Jean-Paul



More information about the Python-list mailing list