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

Python 2.4 and os.open question?

P: n/a
I just test in Windows XP with Python 2.4

I'd like to create a file with exclusive flag.
If file exist I try to use it, if not I'd like to create it.
Python (and underlying library) works differently with/without O_EXCL
flag. Is this okay. How I should use this.

Has somebody manual :-) ?

Eino Mäkitalo

see scenarios (1 without flag ) (2 with flag)

Scenario 1:

To create file if it's not available this works ok
aa=os.open("c:\\temp\\a.txt",os.O_RDWR|os.O_CREAT)
os.close(aa)
aa=os.open("c:\\temp\\a.txt",os.O_RDWR|os.O_CREAT)
os.close(aa)

Scenario 2:
But if you try to do same with O_EXCL then it does not use same logic???
aa=os.open("c:\\temp\\a.txt",os.O_RDWR|os.O_EXCL|o s.O_CREAT)
os.close(aa)
aa=os.open("c:\\temp\\a.txt",os.O_RDWR|os.O_CREAT)

Traceback (most recent call last):
File "<string>", line 1, in <string>
OSError: [Errno 17] File exists: 'c:\\temp\\a.txt'
Jul 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Op 2005-01-11, Eino Mäkitalo schreef <ei**@iki.fi>:
I just test in Windows XP with Python 2.4

I'd like to create a file with exclusive flag.
Why? What is it about the exclusive flag that
makes you like to use it?
If file exist I try to use it, if not I'd like to create it.
If you want that, you cant use the exclusive flag.
Python (and underlying library) works differently with/without O_EXCL
flag.
Well if the absence and presence of this flag wouldn't make a
difference, it would hardly be usefull to have such a flag,
wouldn't it?
Is this okay. How I should use this.

Has somebody manual :-) ?

Eino Mäkitalo

see scenarios (1 without flag ) (2 with flag)

Scenario 1:

To create file if it's not available this works ok
aa=os.open("c:\\temp\\a.txt",os.O_RDWR|os.O_CREAT)
os.close(aa)
aa=os.open("c:\\temp\\a.txt",os.O_RDWR|os.O_CREAT)
os.close(aa)

Scenario 2:
But if you try to do same with O_EXCL then it does not use same logic???
That is what flags are for: to change the logic. O_EXCL, makes sure
you are the one that creats the file. If the file exists it fails.
This is to make sure that if two programs can create the same file
but shouldn't work on it the same time, the file isn't opened
multiple times.
aa=os.open("c:\\temp\\a.txt",os.O_RDWR|os.O_EXCL|o s.O_CREAT)
os.close(aa)
aa=os.open("c:\\temp\\a.txt",os.O_RDWR|os.O_CREAT)


I suppose this should again be the instrcution two lines above;
this actually works. (At least on my linux box, if it didn't on
your XP box, that is a bug)
Traceback (most recent call last):
File "<string>", line 1, in <string>
OSError: [Errno 17] File exists: 'c:\\temp\\a.txt'


Which is exactly as it should, provided you actually used the os.O_EXCL
flag twice.

--
Antoon Pardon
Jul 18 '05 #2

P: n/a
Antoon Pardon wrote:
Why? What is it about the exclusive flag that
makes you like to use it?


Ok. Thanks, I misunderstood the meaning of flag.
What I' like to do is to open file and keep it exclusive locked for me.
Apparently this flag is not for me.

Eino Mäkitalo
Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.