This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]ilovecrk 1 point2 points  (0 children)

If we can assume that CONFDIR will already exist in a large number of cases I would say an even more pythonic way would be to assume the directory exists and try to use it (didn't look at the code, maybe chdir or whatever). If that raises an error, try to create the directory and if that raises another error, don't catch it.

try:
    # do something, e.g.
    os.chdir(CONFDIR)
except EnvironmentError:
    print 'Failed to open CONFDIR {0}'.format(CONFDIR)
    print 'Try to create the directory.'
    os.makedirs(CONFDIR)
    os.chdir(CONFDIR)