By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,771 Members | 1,669 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,771 IT Pros & Developers. It's quick & easy.

How to check whether file is open or not

P: n/a
Ros
There are 10 files in the folder. I wish to process all the files one
by one. But if the files are open or some processing is going on them
then I do not want to disturb that process. In that case I would
ignore processing that particular file and move to next file.

How can I check whether the file is open or not?

I tried os.stat and os.access but I am not getting the expected
results.
Also I checked in IO exceptions, IO error handler doesnt raise any
error if the file is open.
There are some options to check but they are platform dependent (works
only with unix)

Your help would be highly appreciated.

Thanks,
Ros

Mar 7 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Ros wrote:
There are 10 files in the folder. I wish to process all the files one
by one. But if the files are open or some processing is going on them
then I do not want to disturb that process. In that case I would
ignore processing that particular file and move to next file.

How can I check whether the file is open or not?

I tried os.stat and os.access but I am not getting the expected
results.
Also I checked in IO exceptions, IO error handler doesnt raise any
error if the file is open.
There are some options to check but they are platform dependent (works
only with unix)
You can't do that, at least not in a platform agnostic way, and usually not
without cooperation of the file-modifying processes.

Diez
Mar 7 '07 #2

P: n/a
En Wed, 07 Mar 2007 02:28:33 -0300, Ros <py******@gmail.comescribió:
There are 10 files in the folder. I wish to process all the files one
by one. But if the files are open or some processing is going on them
then I do not want to disturb that process. In that case I would
ignore processing that particular file and move to next file.

How can I check whether the file is open or not?

I tried os.stat and os.access but I am not getting the expected
results.
Also I checked in IO exceptions, IO error handler doesnt raise any
error if the file is open.
There are some options to check but they are platform dependent (works
only with unix)
This works only on Windows:
You can use the _sopen function (from the C runtime library), which takes
additional flags for specifying file sharing. See
http://msdn2.microsoft.com/en-us/lib...50(VS.60).aspx
_sopen with _SH_DENYRW will fail if the file is already open by the same
or another process. Try to open the file using this flag: if _sopen
succeeds (does not return -1) the file was not already open (remember to
close it as soon as possible!); if _sopen fails (returns -1) it was
already open.

Using the ctypes module (included with Python 2.5; you can download and
install it for previous versions) you can call that function easily:

pyfrom ctypes import *
pycrt = cdll.msvcrt
py_sopen = crt._sopen
py_sopen.argtypes = (c_char_p, c_int, c_int, c_int)
py_SH_DENYRW = 0x10 # from <share.h>
pyh = _sopen("C:\\1.txt", 0, _SH_DENYRW, 0)
pyh
3
pyh2 = _sopen("C:\\1.txt", 0, _SH_DENYRW, 0)
pyh2
-1
py_close = crt._close
py_close(h)
0
pyh2 = _sopen("C:\\1.txt", 0, _SH_DENYRW, 0)
pyh2
3
py_close(h2)
0

Note: You said "But if the files are open or some processing is going on
them then I do not want to disturb that process.". There exist a (small)
risk of disturbing the other process: if it tries to open the file just
after you opened it, but before you close it, the operation will fail. It
is a tiny window, but might happen...

Surely there are other ways - some programs can report all processes
having a certain file opened, by example, but I don't know how to do that.

--
Gabriel Genellina

Mar 7 '07 #3

P: n/a
Ros wrote:
There are 10 files in the folder. I wish to process all the files one
by one. But if the files are open or some processing is going on them
then I do not want to disturb that process. In that case I would
ignore processing that particular file and move to next file.

How can I check whether the file is open or not?
<snip>

From what I know: You can't, in a platform independent way.

You'd still be in trouble even if python would let you write something like:

if file_is_open(filename):
process_file(filename)
There's nothing that says no one will open the file after the
file_is_open call but before the process_file call. Also, the file might
be opened during the processing.

If the files to be processed only are written and then closed without
being reopened or modified, then your situation is a bit simpler.

If you can get the process that creates the files to cooperate, a common
way to solve this problem is to have a temporary suffix on files while
they are being created. Once the file is complete, the temporary suffix
is removed and that signals to other processes that this file is ready
for processing.

Mar 7 '07 #4

P: n/a
Ros

Thanks a lot Gabriel.
ctypes is working!

Regards,
Ros
Mar 8 '07 #5

P: n/a
On Mar 7, 6:28 am, "Ros" <pyalb...@gmail.comwrote:
There are 10 files in the folder. I wish to process all the files one
by one. But if the files are open or some processing is going on them
then I do not want to disturb that process. In that case I would
ignore processing that particular file and move to next file.
If you can os.open() with O_EXLOCK flag, it's not open by other
process. Now that should work on Unix, Linux and MacOS X. All Windows
proceeding form NT line, are supposed to be POSIX compatible, but
somehow this flag is not available.

Mar 8 '07 #6

P: n/a
En Thu, 08 Mar 2007 11:10:42 -0300, Bart Ogryczak <B.********@gmail.com>
escribió:
On Mar 7, 6:28 am, "Ros" <pyalb...@gmail.comwrote:
>There are 10 files in the folder. I wish to process all the files one
by one. But if the files are open or some processing is going on them
then I do not want to disturb that process. In that case I would
ignore processing that particular file and move to next file.

If you can os.open() with O_EXLOCK flag, it's not open by other
process. Now that should work on Unix, Linux and MacOS X. All Windows
proceeding form NT line, are supposed to be POSIX compatible, but
somehow this flag is not available.
O_EXLOCK is not in POSIX.

--
Gabriel Genellina

Mar 9 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.