OS.MKDIR( ) Overwriting previous folder created...

Discussion in 'Python' started by Ernesto, Feb 2, 2006.

  1. Ernesto

    Ernesto Guest

    I couldn't find this with a search, but isn't there a way to overwrite
    a previous folder (or at least not perform osmkdir( ) if your program
    detects it already exists). Thanks !
     
    Ernesto, Feb 2, 2006
    #1
    1. Advertising

  2. Ernesto

    Ernesto Guest

    Ernesto wrote:
    > I couldn't find this with a search, but isn't there a way to overwrite
    > a previous folder (or at least not perform osmkdir( ) if your program
    > detects it already exists). Thanks !


    I suppose this also leads to the question of:

    "Is there a way to determine if a path exists or not?" Then I could
    do:

    if(path exists){}
    else{mkdir("")}
     
    Ernesto, Feb 2, 2006
    #2
    1. Advertising

  3. Ernesto

    Ernesto Guest

    NEVERMIND ! Here is the solution...

    # ----------------------------------------------------------------
    if (os.path.isdir("C:\\MyNewFolder") == 0):
    os.mkdir("C:\\MyNewFolder")
    # -----------------------------------------------------------------

    thanks
     
    Ernesto, Feb 2, 2006
    #3
  4. Ernesto

    Larry Bates Guest

    try os.path.exists(path)

    -Larry Bates

    Ernesto wrote:
    > Ernesto wrote:
    >> I couldn't find this with a search, but isn't there a way to overwrite
    >> a previous folder (or at least not perform osmkdir( ) if your program
    >> detects it already exists). Thanks !

    >
    > I suppose this also leads to the question of:
    >
    > "Is there a way to determine if a path exists or not?" Then I could
    > do:
    >
    > if(path exists){}
    > else{mkdir("")}
    >
     
    Larry Bates, Feb 2, 2006
    #4
  5. Ernesto wrote:
    > I couldn't find this with a search, but isn't there a way to overwrite
    > a previous folder (or at least not perform osmkdir( ) if your program
    > detects it already exists). Thanks !


    Would something like this help?

    import os

    def failsafe_mkdir(dirname):
    try: os.mkdir(dirname)
    except: return False
    else: return True


    if __name__ == "__main__":
    dirname = 'adir'
    if failsafe_mkdir(dirname):
    print "ok\n"
    else:
    print "couldn't create %s\n" % dirname
     
    Jeffrey Schwab, Feb 3, 2006
    #5
  6. Ernesto

    Guest

    The corect way is to try os.mkdir, catch the exception and check the
    errno value, which tell you why the call failed.

    If the directory exists, you can ignore the exception, if its another
    error, you usually had to raise it again and let the caller handle it.

    Example:
    import errno

    try:
    os.mkdir(path)
    except OSError, err:
    if err.errno != errno.EEXIST:
    raise

    Any other way may have race conditions, for example, you check if the
    directory exits, and its missing, then another process or thread
    creates it before you try to create the missing directory, and your
    mkdir call will raise.
     
    , Feb 3, 2006
    #6
  7. Ernesto <> wrote:
    > NEVERMIND ! Here is the solution...
    >
    > # ----------------------------------------------------------------
    > if (os.path.isdir("C:\\MyNewFolder") == 0):
    > os.mkdir("C:\\MyNewFolder")
    > # -----------------------------------------------------------------


    Maybe some other process creates C:\\MyNewFolder between the call of
    isdir and mkdir, or mkdir fails for some other reasons (e.g. no
    permission), so you have to catch exceptions anyway. But then there's no
    need for isdir.


    Florian
    --
    Das ist ja das positive am usenet: man erfährt oft Dinge, nach denen
    gar nicht gefragt wurde.
    [Konrad Wilhelm in <>]
     
    Florian Diesch, Feb 4, 2006
    #7
    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. Roedy Green

    mkdir

    Roedy Green, Sep 14, 2003, in forum: Java
    Replies:
    1
    Views:
    541
    Harald Hein
    Sep 14, 2003
  2. Thomas Guettler

    os.mkdir() not creating . and ..

    Thomas Guettler, Sep 30, 2003, in forum: Python
    Replies:
    1
    Views:
    396
    Thomas Guettler
    Sep 30, 2003
  3. Shaun
    Replies:
    2
    Views:
    568
    Simon Brunning
    Apr 22, 2005
  4. DataSmash

    batch mkdir using a file list

    DataSmash, Sep 23, 2005, in forum: Python
    Replies:
    7
    Views:
    6,934
    DataSmash
    Sep 23, 2005
  5. ruby talk

    FTP folder creation mkdir problem

    ruby talk, Oct 20, 2004, in forum: Ruby
    Replies:
    3
    Views:
    365
    ruby talk
    Oct 20, 2004
Loading...

Share This Page