473,508 Members | 2,289 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Locking a file under Windows

Hi all,

I'm working on an application that will be used by several users at the same
time. The user should be able to both read and write to some data file
stored on our file server. My question is: how can I prevent that one user
writes to the file while another user is reading it?

I've seen some info on this in the fcntl module, but this module is not
available under Windows, so I need something else. Here is what I've come up
with so far:

def WriteToFile(filename):
for i in range(max_nr_tries):
try:
fd = os.open(filename, os.O_EXCL | os.O_WRONLY)
except OSError, (error, message):
if error == errno.ENOENT:
# create first
try:
fd = os.open(filename, os.O_CREAT | os.O_EXCL |
os.O_WRONLY)
break
except:
print "Oops!"
else:
# try again later
time.sleep(1)
continue
time.sleep(20)
# + write & close file

I thought that, when this function gets to the last sleep(), I'd have an
exclusive lock on the file. However, it seems I can still open the same file
for reading/writing in another process (even os.O_EXCL).

Is this normal? What am I missing? How can I open a file exclusively (for
writing)?

Thanks,
g
Nov 24 '05 #1
0 903

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

Similar topics

3
10383
by: dchadha | last post by:
Hi, I am working on application in C# which uses and stores data in xml file. This app can use xml file from two different ways: (a) From User Interface (Windows application) and (b) From...
0
1479
by: JM | last post by:
I am working on an application which uses xml file as a database. The application has 2 interfaces which accesses this xml file. "Windows application written in C#" and "Windows Service written in...
5
4504
by: JM | last post by:
I have created a Windows Service which uses xml file as a data source. The service is running on .NET 2.0 and uses LocalSystem account. It was running fine but now it has started locking the xml...
6
3019
by: joseph2000 | last post by:
Hi, I'd like to ask you for some ideas how to solve problem I currently have. The problem is as follows: we have a component which is integrated with w Windows Explorer. From time to time we...
2
1012
by: Sparky | last post by:
Hello! I am writing some software that will have many users accessing the same file resource at once for reading purposes only. I am programming on (Ubuntu) Linux and my question is in Windows, can...
0
7120
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
7323
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
7380
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...
1
7039
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
5626
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
4706
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
3192
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3180
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1553
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 ...

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.