I have code that makes a folder and places output files in it. I want to use a try-except-else block and an overwrite option, which can be set to True or False, so that in the case where the folder already exists and overwrite is set to false it will just print that the folder already exists, and in all other cases it will just execute without comment.
The only solution I've come up with so far looks like this:
def function( parameters, overwrite = False ):
try:
os.makedirs( dir )
except OSError:
if overwrite:
data making code...
else:
print dir + ' already exists, skipping...'
else:
if overwrite:
data making code...
Is there maybe a better, or just more elegant solution to this problem? Like, for example, one in which I don't have to duplicate my data making code? Doing it this way reminds me too much of the style in which I've ended up having to write some things in C, and doesn't seem very Pythonic.