I made this python script to create dated folders within a directory. This is my first practical automation project and I don't want to cement any bad habits so I was wondering if there is anything I can improve in the code I have written here. Any feedback will be appreciated!
The main challenge I had was to account for months with different day ranges, and then to reset the counts properly for all the variables. I feel like this is more complicated than it probably needs to be but I don't know if there are some python libraries I am unaware of that could help simplify it more.
I thought about having a predefined end date as well but that would require the user to open up a calendar and find 2 dates. I figured by just asking for a start date and how many folders they want, I could make things simpler for the end user but I'm not even sure that that is actually the case.
I also don't know if I've used classes correctly here. At first I had everything thrown into the while statement. I also considered using a for loop instead of while but went with while instead. Again, this is my first real project so any help with other decisions I could have made will be appreciated!
import os import pathlib directory = "/Users/myName/Box Sync/SAFAC/Budget Requests/2020-2021/Weekly/" # directory = input("Enter the path where you would like to create files: ") date = input("Enter the starting date in mm-dd-yyyy format: ") period = int(input("Enter the period between each date folder: ")) instances = int(input("How many folders do you want? ")) Folders = [] arguments = date.split("-") dayArgument = arguments[1] monthArgument = arguments[0] yearArgument = arguments[2] init = instances - instances elapse = period - period seperator = "-" month = int(monthArgument) day = int(dayArgument) + int(elapse) def make_directories(input_list): for folderName in input_list: dirpath = os.path.join(directory, folderName[0:],'Documentation') try: os.makedirs(dirpath) except FileExistsError: print('Directory {} already exists'.format(dirpath)) else: print('Directory {} created'.format(dirpath)) def add_folder_to_list(month,day,year,extra): full_name_constructor = (month,day,year,extra) # full_name_constructor = (str(month),str('0'+str(day)),yearArgument,"Budgets") fullName = seperator.join(full_name_constructor) Folders.append(fullName) def format_and_append_folder_name(): if day < 10 and month >= 10: add_folder_to_list(str(month),str('0'+str(day)),yearArgument,"Budgets") elif day >= 10 and month < 10: add_folder_to_list(str('0'+str(month)),str(day),yearArgument,"Budgets") elif day < 10 and month < 10: add_folder_to_list(str('0'+str(month)),str('0'+str(day)),yearArgument,"Budgets") else: add_folder_to_list(str(month),str(day),yearArgument,"Budgets") def check_month(): global maxDays if (month % 2 ) == 0 and month != 2 : # if even but not February maxDays = 30 elif month == 2 : maxDays = 28 #if February else : maxDays = 31 # if odd while init < instances : check_month() format_and_append_folder_name() make_directories(Folders) # print(Folders[init]) # for debugging init += 1 elapse += period day = int(dayArgument) + int(elapse) if day > maxDays : month += 1 day -= maxDays # reset days dayArgument = day # recalibrate the new zero point for the day to be the date in the new month elapse = 0 # recalibrate elapse point to 0 so it can reiterate the period again