【问题标题】:python snapshot program errorpython快照程序错误
【发布时间】:2013-04-15 23:46:28
【问题描述】:

我正在运行这个程序来拍摄我的屏幕快照并保存它,但收到一条错误消息

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)

这是错误信息:

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

【问题讨论】:

    标签: python message screenshot


    【解决方案1】:

    您的问题是strftime 格式字符串中的%_ - 它不是有效值。将格式字符串替换为以下内容就可以了:

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

    您可以判断格式字符串有问题,因为引发的错误 (ValueError: Invalid format string) 引用了格式字符串,而第 10 行中唯一的格式字符串就是这个。您可以检查documentation 以查看哪些字符是有效实体。 %_ 不是其中之一,我猜这只是一个印刷错误。

    【讨论】:

    • 很高兴为您提供帮助!如果其中一个答案解决了您的问题,您应该接受它。
    【解决方案2】:

    时间有点少:

    str_time = time.strftime(your_real_date,'%Y_%m_%d_%H_%M_%S')
    saveas=os.path.join(SaveDirectory,'ScreenShot_'+ str_time +'.png')
    

    并且每次都检查一下 Python 文档:8.1.7. strftime() and strptime() Behavior 以确保使用任何字符串/日期转换

    【讨论】:

      猜你喜欢
      • 2016-12-19
      • 2012-08-11
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      • 2023-02-25
      • 2018-06-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多