parsed.org

Tips by tag: mkdir

Create Directories Cleanly by cygnus on Jan 24, 2005 09:24 AM

os.mkdir raises a generic exception if an error occurs, but typically you need to ignore preexisting directory errors. Here's how to ignore them:

import os, errno, sys

myPath = "/path/to/dir"
try:
    os.mkdir(myPath)
except Exception, e:
    code, st = e
    if code != errno.EEXIST:
        st = "Error creating directory '%s': %s" % (myPath, str(e))
        sys.exit(1)
directoryexceptionslanguagesmkdirosprogrammingpython
RSS