Search code examples
pythonpyautogui

Using Python module pyautogui, to take a screenshot and auto name the .png file by date&time


I am creating a program that will eventually read the screen through shots and I need it to save the filename with a date and time stamp, the program runs fine but does not create the filename correctly and is therefore replacing the existing screenshot. How do I change it to where the file name becomes the date and time defined in the code below?

def getTime():



    #GET THE CURRENT DATE AND TIME
    now = datetime.datetime.now()
    now_str = now.strftime("%Y-%m-%d-%H-%M-%S")
    outFile = pyautogui.screenshot('ImageFile.PNG'.format(now_str))

Solution

  • You are missing the brackets. https://docs.python.org/2/library/string.html#format-examples

    outFile = pyautogui.screenshot('ImageFile{}.PNG'.format(now_str))