Fwd: is file open in system ? - other than lsof

A.T.Hofkamp hat at se-162.se.wtb.tue.nl
Fri Apr 18 07:10:35 EDT 2008


On 2008-04-17, bvidinli <bvidinli at gmail.com> wrote:
> is there another way, any python command sequence that i can check if
> a file is open at the time of "before i process file"
>
> i am not interested in " the file may be written after i access it.."
> the important point is " the time at i first access it."
>
> my routine is something like:
> for i in listoffiles:
> checkfileopen(i)
> processfile()

This code does not give you what you are after; in between 'checkfileopen()' and
'processfile()' somebody may open the file and mess with it.

I quite doubt that you can get what you want, at OS level.

Even if you get exclusive open(), you are not safe imho.
After you opened the file for access (and before you perform the read() call),
another process may also open it, and alter the data while you are reading.
The same may happen between 2 read() calls.

Note that a read() at OS level is not the same as a read() at Python (or C
fread()) level. Python pretends to read an entire file in one call, but the OS
is using disk-blocks of the underlying file system as unit of read/write.


In other words, even if nobody messes with the file at the moment you open it,
you don't necessarily get an uncorrupted file afaik.

Sincerely,
Albert



More information about the Python-list mailing list