title: Extract Directory Names from Dynamic File Paths in Python
body: <p>Is there a way in Python to extract each file directory and the file in a windows file path either through the use of <code>REGEX</code> and <code>group()</code> or <code>os.path</code>? </p>
  
  <p>I'm dealing with the file paths that have varying amount of directories, so one line could be <code>D:\dir1\file.txt</code> while the next could be <code>Z:\dir1\dir2\dir3\dir4\dir5\file.txt</code>, so I wonder if there is even a way to do this with <code>REGEX</code> or a built in function in Python when there are varying amounts of <code>\</code> in the text I'm searching.</p>
  
  <p>Any insight would be helpful, even if it's just the bitter truth that it can't be done.</p>
  
  <p>After Edit:</p>
  
  <p>I'm trying to extract the directory names in between the <code>\</code> and the final <code>file.txt</code> and write each <code>dir</code> or <code>file</code> to it's own column in an output text file.</p>
  
  <p>My desired output for the above two lines would be:</p>
  
  <pre><code> col1|col2|col3|col4|col5|col6
   dir1|dir2|dir3|dir4|dir5|file.txt
   dir1|    |    |    |    |file.txt
  </code></pre>
  
  <p>I know <code>os.path</code> has a lot of good built in functions, but after reading this site: <a href="https://docs.python.org/2/library/os.path.html" rel="nofollow noreferrer">https://docs.python.org/2/library/os.path.html</a>, I don't think any of them are doing what I'm trying to do.</p>