473,396 Members | 2,037 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to check whether file is open or not

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
6 34604
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
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
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
Ros

Thanks a lot Gabriel.
ctypes is working!

Regards,
Ros
Mar 8 '07 #5
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: mark4asp | last post by:
Hello, Is there a simple way to check whether a database driver is available on a remote server? I only have ftp access so I can only run ASP scripts. An ASP application on a host has...
4
by: Rob | last post by:
I have an Access application that allows users to open up certain files from within Access (.doc files with automation, .pdf's with a pdf viewing form). Other formats, we want to use Internet...
19
by: wetherbean | last post by:
Hi group..I am writing a playlist management protocol where I have a file that holds all the playlists and a file that holds all the songs....before a playlist is created I need to check to see if...
10
by: Raymond | last post by:
Hi All: To find a file exists using the file name, I have tow routings on UNIX system. 1. access(2) 2. lstat(2) This tow function also can do. When the return value is "-1" and errno is...
7
by: andylcx | last post by:
Hi all: The c++ language can check whether the file exist or not. I am wondering how c language does this job? Thanks a lot! Andy
5
by: sword | last post by:
How can I check whether a file exists?
4
by: softwareakash | last post by:
Hi I have a dll which logs some values in a text file. In this I am initialising some values as soon as the object gets called like this public class Logger { // Initialising variables from...
2
by: Kimmo Laine | last post by:
Hi! Is there a way to check if file is already open/used by another process? I know that i can do something like this to check it try { StreamWriter sw = new StreamWriter(filename);...
3
by: Mayur H Chauhan | last post by:
All, I am working on one product where we need to check for physical file is in use (if it is open by other application). What I found out is that I am able to trap for those files whose attribute...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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
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,...

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.