Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > OS.MKDIR( ) Overwriting previous folder created...

Reply
Thread Tools

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

 
 
Ernesto
Guest
Posts: n/a
 
      02-02-2006
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 !

 
Reply With Quote
 
 
 
 
Ernesto
Guest
Posts: n/a
 
      02-02-2006

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

 
Reply With Quote
 
 
 
 
Ernesto
Guest
Posts: n/a
 
      02-02-2006
NEVERMIND ! Here is the solution...

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

thanks

 
Reply With Quote
 
Larry Bates
Guest
Posts: n/a
 
      02-02-2006
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("")}
>

 
Reply With Quote
 
Jeffrey Schwab
Guest
Posts: n/a
 
      02-03-2006
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
 
Reply With Quote
 
nirsof@gmail.com
Guest
Posts: n/a
 
      02-03-2006
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.

 
Reply With Quote
 
Florian Diesch
Guest
Posts: n/a
 
      02-04-2006
Ernesto <(E-Mail Removed)> 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 <(E-Mail Removed)>]
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
help: how can i use my previous XP folder? Tron General Computer Support 0 08-28-2010 11:48 AM
..HtmlControls.HtmlInputFile.Saveas(filename) not overwriting =?Utf-8?B?UmVkZHk=?= ASP .Net 5 12-28-2005 07:58 PM
overwriting fields from abstract class Peter Grison Java 3 04-27-2004 05:14 PM
Java concurrent transactions overwriting Dave Ficken Java 1 11-10-2003 08:25 PM
AXIS and Java - concurrent transactions overwriting each other Dave Ficken Java 0 11-08-2003 02:19 PM



Advertisments