What is the most elegant way to check if the directory a file is going to be written to exists, and if not, create the directory using Python? Here is what I tried:
filename = "/my/directory/filename.txt"
dir = os.path.dirname(filename)
f = file(filename)
Somehow, I missed os.path.exists . This is what I have now:
d = os.path.dirname(f)
if not os.path.exists(d):
Is there a flag for "open", that makes this happen automatically?