Every month my department has to generate blank upload files (xlsx) for each day of the month.
The following code helps this process by copying a template, iterating through the number of days of the current month, and generating a file for each of those days from the template.
I found the
copy_rename function which helped a ton.
I'm looking for any feedback pertaining to:
- Can I avoid the
- Do I need to
.close()the file, since I didn't open it?
- Layout of the code: can I have my functions at the very bottom so that the code comes up first?
# checks whether a number is single digit or not def format_Num(num): num = str(num) if len(num) == 1: return(str(0) + str(num)) else: return str(num) #function copies a file from one dir, to another def copy_rename(old_file_name, new_file_name): src_dir= "C:\\a\\dir\\Desktop" dst_dir= "C:\\a\\different\\dir\\TESTPYTHONFILES" src_file = os.path.join(src_dir, old_file_name) shutil.copy(src_file,dst_dir) dst_file = os.path.join(dst_dir, old_file_name) new_dst_file_name = os.path.join(dst_dir, new_file_name) os.rename(dst_file, new_dst_file_name) from calendar import monthrange import shutil import os import datetime now = datetime.datetime.now() curr_year = now.year #2018 curr_month = now.month #04 total_month_days = monthrange(curr_year, curr_month) + 1 filepath = "C:\\a\\dir\\Desktop" template = "Template.xlsx" for day in range(1, total_month_days): wb_Name = "workFeed" + str(curr_year) + format_Num(curr_month) + format_Num(day) + ".xlsx" path_n_fileName = os.path.join(filepath + wb_Name) # verifies that the file does not exist if os.path.isfile(path_n_fileName) != True: copy_rename(template, wb_Name)