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:
- how to get the current date (string) in yyyymmdd format
- how to create a new directory if it doesn't already exist
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
- Google search for
python get current date yyyymmdd
- The top search result was a Stack Overflow answer with > 1000 upvotes (which indicates a broadly approved answer that should be reliable).
- Note that the date format was slightly different in this question/answer (
yyyy-mm-dd
) -- I omitted the hyphens in my code, to get the desired format yyyymmdd
.
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:
# specify path
path = 'c:/users/kbrandl/desktop/' + current_date
Step 2: Determine how to create a directory (if it doesn't already exist) in Python
- Google search for
python create folder if not exists
- Once again, the top search result provided the sample code I was looking for.
With this info, I now know how to create my target directory (folder) if it doesn't yet exist:
# create directory if it doesn't exist
if not os.path.exists(path):
os.mkdir(path)
Putting this all together now...the following code achieves your stated objective.
import os, smartsheet
from datetime import datetime
sheetId = 3932034054809476
# get current date in yyyymmdd format
current_date = datetime.today().strftime('%Y%m%d')
# specify path
path = 'c:/users/kbrandl/desktop/' + current_date
# create directory if it doesn't exist
if not os.path.exists(path):
os.mkdir(path)
# download file to specified path
smartsheet_client.Sheets.get_sheet_as_excel(
sheetId,
path,
'MyFileName.xlsx'
)