473,509 Members | 3,095 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

is file open in system ? - other than lsof

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.
--
Ý.Bahattin Vidinli
Elk-Elektronik Müh.
-------------------
iletisim bilgileri (Tercih sirasina gore):
skype: bvidinli (sesli gorusme icin, www.skype.com)
msn: bv******@iyibirisi.com
yahoo: bvidinli

+90.532.7990607
+90.505.5667711
Jun 27 '08 #1
5 5910
On 2008-04-16, bvidinli <bv******@gmail.comwrote:
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.
Sincerely,
Albert
Jun 27 '08 #2
On 16-Apr-08, at 9:20 AM, A.T.Hofkamp wrote:
On 2008-04-16, bvidinli <bv******@gmail.comwrote:
>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
Jun 27 '08 #3
bvidinli schrieb:
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.

On Linux there are symlinks from /proc/PID/fd to the open
files. You could use this:

#!/usr/bin/env python
import os
pids=os.listdir('/proc')
for pid in sorted(pids):
try:
int(pid)
except ValueError:
continue
fd_dir=os.path.join('/proc', pid, 'fd')
for file in os.listdir(fd_dir):
try:
link=os.readlink(os.path.join(fd_dir, file))
except OSError:
continue
print pid, link
--
Thomas Guettler, http://www.thomas-guettler.de/
E-Mail: guettli (*) thomas-guettler + de
Jun 27 '08 #4
Thomas Guettler <hv@tbz-pariv.dewrote:
bvidinli schrieb:
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.

On Linux there are symlinks from /proc/PID/fd to the open
files. You could use this:

#!/usr/bin/env python
import os
pids=os.listdir('/proc')
for pid in sorted(pids):
try:
int(pid)
except ValueError:
continue
fd_dir=os.path.join('/proc', pid, 'fd')
for file in os.listdir(fd_dir):
try:
link=os.readlink(os.path.join(fd_dir, file))
except OSError:
continue
print pid, link
Unfortunately I think that is pretty much exactly what lsof does so is
unlikely to be any faster!

However if you have 1000s of files to check you only need to do the
above scan once and build a dict with all open files in whereas lsof
will do it once per call so that may be a useful speedup.

Eg

------------------------------------------------------------
import os
pids=os.listdir('/proc')
open_files = {}
for pid in sorted(pids):
try:
int(pid)
except ValueError:
continue
fd_dir=os.path.join('/proc', pid, 'fd')
try:
fds = os.listdir(fd_dir)
except OSError:
continue
for file in fds:
try:
link=os.readlink(os.path.join(fd_dir, file))
except OSError:
continue
if not os.path.exists(link):
continue
open_files.setdefault(link, []).append(pid)

for link in sorted(open_files.keys()):
print "%s : %s" % (link, ", ".join(map(str, open_files[link])))
------------------------------------------------------------
You might want to consider http://pyinotify.sourceforge.net/ depending
on exactly what you are doing...

--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
Jun 27 '08 #5
On 2008-04-17, bvidinli <bv******@gmail.comwrote:
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
Jun 27 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
2092
by: Ignacio X. Domínguez | last post by:
Hi. I'm developing a desktop application that needs to store some data in a local file. Let's say for example that I want to have an address book with names and phone numbers in a file. I would...
13
4281
by: Sky Sigal | last post by:
I have created an IHttpHandler that waits for uploads as attachments for a webmail interface, and saves it to a directory that is defined in config.xml. My question is the following: assuming...
3
3960
by: Thomas Bartkus | last post by:
This may be more of a Linux question, but I'm doing this from Python. ..... How can I know if anything (I don't care who or what!) is in the middle of using a particular file? This comes in...
9
10245
by: Mark | last post by:
Hi all, This is something which has been bugging me for ages. How can I check if a file is already in use by a different program? It doesn't seem to matter which mode I pass to fopen, it will...
14
2779
by: prasadjoshi124 | last post by:
Hi All, I am writing a small tool which is supposed to fill the filesystem to a specified percent. For, that I need to read how much the file system is full in percent, like the output given...
19
5356
by: Lee Crabtree | last post by:
Is there a class in the framework that allows me read text from a file in an unbuffered manner? That is, I'd like to be able to read lines in the same manner as StreamReader.ReadLine(), but I also...
4
2111
by: MikeJ | last post by:
make a While loop ofs = TextFileServer("somefile") string srow while (ofs=false) { srow=ofs.getRow(); Console.Writeline(srow); }
0
998
by: paul | last post by:
bvidinli schrieb: I think you can do this with inotify. It's an event based notification mechanism for linux kernel 2.6.13 and up. It has python bindings available (google for pyinotify). You...
13
10568
by: writeson | last post by:
Hi all, I'm writing some code that monitors a directory for the appearance of files from a workflow. When those files appear I write a command file to a device that tells the device how to...
0
7234
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7344
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7505
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5652
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4730
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3203
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1570
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
775
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
441
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.