471,305 Members | 1,468 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Python 2.4 and os.open question?

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
2 4770
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
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.

Similar topics

13 posts views Thread by Allison Bailey | last post: by
3 posts views Thread by Tim Gahnstrom | last post: by
20 posts views Thread by Ilias Lazaridis | last post: by
1 post views Thread by Fouff | last post: by
50 posts views Thread by diffuser78 | last post: by
158 posts views Thread by Giovanni Bajo | last post: by
4 posts views Thread by Martitza | last post: by

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.