, :
if not os.path.exists(dirmk):
print "Creating: {}".format(dirmk)
os.makedirs(dirmk)
else:
print "{} already exists; skipping".format(dirmk)
You are already trying to use os.path.exists(), so use it in a state.
If the path does not exist; create it and print a message; otherwise skip.
An alternative is to use try/except/else.
Update: Of course, I can understand why you can use it try/except/elsehere, but IHMO you call os.makedirs()regardless of whether the path exists or not. So I would do this as an improvement for catch and OSError(s):
if not os.path.exists(dirmk):
print "Creating: {}".format(dirmk)
try:
os.makedirs(dirmk)
except:
print "Failed to create {}".format(dirmk)
else:
print "{} already exists; skipping".format(dirmk)
And if it were written as a reuse function, you could short-circuit this:
def create_if_not_exists(dirmk):
if os.path.exists(dirmk):
print "{} already exists; skipping".format(dirmk)
return
print "Creating: {}".format(dirmk)
try:
os.makedirs(dirmk)
except:
print "Failed to create {}".format(dirmk)
source
share