How to programmatically discern whether or not a file is use

Donn Cave donn at drizzle.com
Sat May 31 12:26:12 EDT 2003


Quoth techead at technetos.com (Techead):
| How can a Python program determine whether or not a file
| is use by another process?  I was hoping to locate a function
| in the standard Python distribution but so far have come up
| blank.

That is because there isn't a C function, i.e., it isn't supported
by the operating system (at least not in a standard way, defined in
something like X/Open XPG4.)

The "lsof" application can find information like this in the kernel
data structures.  If you have or can build lsof, and you need this
information bad enough that it's really worth the trouble, you can
invoke lsof and read its output.

| On the side, is there a way to have a similar function return
| the UID of the account that has the file open?  Or the PID of
| the process that has the file open?
|
| uid = whohas( path_to_my_file )
| pid = whathas( path_to_my_file )

No, but lsof can do that too.

	Donn Cave, donn at drizzle.com




More information about the Python-list mailing list