I'm trying to set a variable to point to a file on the desktop that's generated and named containing today's date. But I'm getting the following error:
IndexError: Replacement index 1 out of range for positional args tuple
The file is to be attached to an Outlook email. I know pathlib requires an absolute path to be stated, but what if I'd like to use format() to make it point to a file whose name changes daily when it's generated?
This is an automation task, so it would be ideal if I could make the date reflect today's date without having to make changes to the code. Thanks in advance!
import win32com.client as client
import pathlib
import datetime
x = datetime.datetime.now()
image_path = pathlib.Path('C:/Users/username/Desktop/Folder {}/Filename {}.png'.format(x.strftime("%b %d")))
If you use empty {}
as placeholders, it is expected that each has to be replaced by a different value, so you'll need to provide as many replacements in format
.
If you want to use the same value twice, you can number them so that both use the argument to format
with index 0:
import pathlib
import datetime
x = datetime.datetime.now()
image_path = pathlib.Path('C:/Users/username/Desktop/Folder {0}/Filename {0}.png'.format(x.strftime("%b %d")))
print(image_path)
# C:/Users/username/Desktop/Folder Aug 08/Filename Aug 08.png