I have the below code which downloads a sheet to a folder on my computer. How do I have it download the excel sheet to a newly created folder with the current day's datestamp? So for example, I want the file to download to a folder called:
C:/Users/E29853/OneDrive/Smartsheets/Templates/20220610/
for any files downloaded on June 10, 2022.
This is the code I have:
import os, smartsheet
token=os.environ['SMARTSHEET_ACCESS_TOKEN']
smartsheet_client = smartsheet.Smartsheet(token)
smartsheet_client.errors_as_exceptions(True)
smartsheet_client.Sheets.get_sheet_as_excel(
8729488427892475,
'C:/Users/E29853/OneDrive/Smartsheets/Templates',
'Region.xlsx'
)
In order to augment your existing code to achieve your stated objective, you need to know how to achieve the following two things with Python:
I'm fairly new to Python myself, but was able to figure this out thanks to Google. In case it's helpful for you in the future, here was my process for figuring this out.
Step 1: Determine how to get the current date (yyyymmdd) in Python
python get current date yyyymmdd
yyyy-mm-dd
) -- I omitted the hyphens in my code, to get the desired formatyyyymmdd
.Now that I know how to get the date string in the desired format, I'll be able to concatenate it with the string that represents my base path, to get my target path:
Step 2: Determine how to create a directory (if it doesn't already exist) in Python
python create folder if not exists
With this info, I now know how to create my target directory (folder) if it doesn't yet exist:
Putting this all together now...the following code achieves your stated objective.