Python file path based on dates
<p>Creating a script that will automatically process the file path for that day, perform some actions, and then save to the same directory. </p>

<p>Folder structure is based on Date:</p>

<p><code>maindir</code> -> <code>year</code> -> <code>month</code> -> files for that month.</p>

<p>So far, my approach would be:</p>

<pre><code>year = time.strftime("%Y")
month = time.strftime("%B")
dir = parse('C:\maindir' + '\' + str(year) + '\' + str(month))
os.chdir(dir)
</code></pre>

<p>However, I would like to reuse this with <code>os.makedirs</code> later so that the folder structure will be automatically generated as I go.</p>

<p>Or would it be better to make a method which parses the dir path so that I can call that as in:</p>

<pre><code>if not os.path.exists(method):
    try:
        os.makedirs(os.path.dirname(method))
</code></pre>

<p><strong>Update:</strong></p>

<p>Happened to find this discussion which helped a lot - <a href="http://stackoverflow.com/questions/17429044/constructing-absolute-path-with-os-join">constructing absolute path with os.join()</a></p>

<pre><code>netdrive="\\network.com\mainfolder$"
year = time.strftime("%Y")
month = time.strftime("%B")
path=path.abspath(path.join(os.sep,netdrive,year,month))
if not os.path.exists(path):
    os.makedirs(path)
os.chdir(path)
</code></pre>

<p>So, I have made some progress with this - however, now I'm having the issue of recognizing the net drive as currently this uses the <code>C:/</code> default as part of the <code>path.abspath</code>. How can I override that to a new root drive independent of what the drive is mapped to? <code>D:/</code> for one user, <code>E:/</code> for second - etc.</p>

<p>Since the path contains only Year and Month,  the directory would remain same for 30 consecutive days on average.</p>

<p>Hence it would be better to check if the directory already exists, before creating it.</p>

<pre><code>if not os.path.exists(directory):
   os.makedirs(directory)
</code></pre>

python windows