This is only a question regarding which one would be more "pythonic"
using if:
import os
somepath = 'c:\\somedir'
filepath = '%s\\thefile.txt' % somepath
if not os.path.exists(somepath) and not os.path.isfile(filepath):
os.makedirs(somepath)
open(filepath, 'a').close
else:
print "file and dir allready exists"
or using try/Except:
import os
somepath = 'c:\\somedir'
filepath = '%s\\thefile.txt' % somepath
try:
os.makedirs(somepath)
except:
print "dir allready exists"
try:
with open(filepath):
// do something
except:
print "file doens't exist"
As you can see on the examples above, which one would be more correct on python? Also, On which cases should i use try/except instead if/else ? I mean, should i replace all my if/else tests to try/except?
Thanks in advance.