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

E

Ernesto

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 !
 
E

Ernesto

Ernesto said:
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("")}
 
E

Ernesto

NEVERMIND ! Here is the solution...

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

thanks
 
L

Larry Bates

try os.path.exists(path)

-Larry Bates
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("")}
 
J

Jeffrey Schwab

Ernesto said:
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
 
N

nirsof

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.
 
F

Florian Diesch

Ernesto said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top