is file open in system ? - other than lsof

Chris McAloney mcaloney at gmail.com
Wed Apr 16 10:00:26 EDT 2008


On 16-Apr-08, at 9:20 AM, A.T.Hofkamp wrote:
> On 2008-04-16, bvidinli <bvidinli at gmail.com> wrote:
>> is there a way to find out if file open in system ? -
>> please write if you know a way  other than lsof. because lsof if  
>> slow for me.
>> i need a faster way.
>> i deal with thousands of files... so, i need a faster / python way  
>> for this.
>> thanks.
>
> This is not a Python question but an OS question.
> (Python is not going to deliver what the OS doesn't provide).
>
> Please first find an alternative way at OS level (ie ask this  
> question at an
> appropiate OS news group). Once you have found that, you can think  
> about Python
> support for that alternative.

I agree with Albert that this is very operating-system specific.   
Since you mentioned 'lsof', I'll assume that you are at least using a  
Unix variant, meaning that the fcntl module will be available to you,  
so you can check if the file is already locked.

Beyond that, I think more information on your application would be  
necessary before we could give you a solid answer.  Do you only need  
to know if the file is open, or do you want only the files that are  
open for writing?  If you only care about the files that are open for  
writing, then checking for a write-lock with fcntl will probably do  
the trick. Are you planning to check all of the "thousands of files"   
individually to determine if they're open?  If so, I think it's  
unlikely that doing this from Python will actually be faster than a  
single 'lsof' call.

If you're on Linux, you might also want to have a look at the /proc  
directory tree ("man proc"), as this is where lsof gets its  
information from on Linux machines.

Chris



More information about the Python-list mailing list