Python 2.4 and os.open question?

Antoon Pardon apardon at forel.vub.ac.be
Tue Jan 11 10:04:44 EST 2005


Op 2005-01-11, Eino Mäkitalo schreef <eino at 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|os.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



More information about the Python-list mailing list