Search code examples
pythonmessagescreenshot

python snapshot program error


Im running this program to take a snapshot of my screen and save it but getting an error message

import os
import sys
import time
import Image
import ImageGrab

SaveDirectory=r'C:\Documents and Settings\User\My Documents\My Pictures'
ImageEditorPath=r'C:\WINDOWS\system32\mspaint.exe'
img=ImageGrab.grab()
saveas=os.path.join(SaveDirectory,'ScreenShot_'+time.strftime('%Y_%m_%d%_%H_%M_%S')+'.png')
img.save(saveas)
editorstring='""%s" "%s"'% (ImageEditorPath,saveas) 
os.system(editorstring)

this is the Error Message:

Traceback (most recent call last):
  File "C:/Python27/butt", line 10, in <module>
    saveas=os.path.join(SaveDirectory,'ScreenShot_'+time.strftime('%Y_%m_%d%_%H_%M_%S')+'.png')
ValueError: Invalid format string

Solution

  • Your problem is the %_ in your format string for strftime - it's not a valid value. Replace the format string with the following and you should be fine:

    '%Y_%m_%d_%H_%M_%S'

    You can tell the format string is at fault because the error raised (ValueError: Invalid format string) refers to a format string, and the only format string in line 10 is this one. You can check the documentation to see what characters would be valid entities. %_ is not one of them, and I'm guessing that it was just a typographical error.