Reputation: 2763
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.
Upvotes: 0
Views: 613
Reputation: 1932
Since the path contains only Year and Month, the directory would remain same for 30 consecutive days on average.
Hence it would be better to check if the directory already exists, before creating it.
if not os.path.exists(directory):
os.makedirs(directory)
Upvotes: 1