Python 2.4 and os.open question?

Discussion in 'Python' started by =?ISO-8859-1?Q?Eino_M=E4kitalo?=, Jan 11, 2005.

  1. 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|os.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'
     
    =?ISO-8859-1?Q?Eino_M=E4kitalo?=, Jan 11, 2005
    #1
    1. Advertising

  2. Op 2005-01-11, Eino Mäkitalo schreef <>:
    > 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
     
    Antoon Pardon, Jan 11, 2005
    #2
    1. Advertising

  3. 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
     
    =?ISO-8859-1?Q?Eino_M=E4kitalo?=, Jan 11, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Shilpa
    Replies:
    1
    Views:
    6,046
    Anubhav Jain
    Mar 22, 2006
  2. Simon Mullis
    Replies:
    0
    Views:
    615
    Simon Mullis
    Feb 5, 2009
  3. Scott

    open-uri and Kernel.open

    Scott, Aug 19, 2005, in forum: Ruby
    Replies:
    3
    Views:
    185
    Robert Klemme
    Aug 22, 2005
  4. Schüle Daniel

    File::open and File.open

    Schüle Daniel, Oct 23, 2006, in forum: Ruby
    Replies:
    4
    Views:
    192
    Rick DeNatale
    Oct 24, 2006
  5. iMelody Ooo
    Replies:
    5
    Views:
    419
    Charles Calvert
    Oct 21, 2010
Loading...

Share This Page