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:

  1. Can I avoid the format_Num function somehow?
  2. Do I need to .close() the file, since I didn't open it?
  3. 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))
    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)

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] + 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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.