Creating a script that will automatically process the file path for that day, perform some actions, and then save to the same directory.
Folder structure is based on Date:
maindir
-> year
-> month
-> files for that month.
So far, my approach would be:
year = time.strftime("%Y")
month = time.strftime("%B")
dir = parse('C:\maindir' + '\' + str(year) + '\' + str(month))
os.chdir(dir)
However, I would like to reuse this with os.makedirs
later so that the folder structure will be automatically generated as I go.
Or would it be better to make a method which parses the dir path so that I can call that as in:
if not os.path.exists(method):
try:
os.makedirs(os.path.dirname(method))
Update:
Happened to find this discussion which helped a lot - constructing absolute path with os.join()
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)
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 C:/
default as part of the path.abspath
. How can I override that to a new root drive independent of what the drive is mapped to? D:/
for one user, E:/
for second - etc.