title: Making directories recursively in python
body: <p>I need to create a file with python, in the directory:</p>
  
  <pre><code>foo/bar/baz/filename.fil
  </code></pre>
  
  <p>The only problem, is that I don't know if baz, bar, or even foo have been created (they may have been, but the script doesn't guarantee it).  So, obiously I can't do simply:</p>
  
  <pre><code>file = open('foo/bar/baz/filename.fil', 'wb')
  # Stuff
  # file.close()
  </code></pre>
  
  <p>because I will get an IOError if foo or bar or baz doesn't exist.  So, I was thinking I could write a script that would</p>
  
  <pre><code>1.  Through a loop of os.path.split()s, get each directory.
  2.  In a loop:  Test to see if each directory exists:
  3.       If it doesn't: make it
  4.  Then write the file.
  </code></pre>
  
  <p>However, it seems like python should have a better way of doing this, so am I missing something, or is the only (or best) way to do it is the algorithm I listed above?</p>
  
  <p>Thank you.</p>