【问题标题】:I am trying to make a script which updates wallpapers. It is giving my OSError我正在尝试制作一个更新壁纸的脚本。它给了我的 OSError
【发布时间】:2021-01-07 00:09:06
【问题描述】:

这是我的代码中的一个 sn-p:

def set_background(image):
    ctypes.windll.user32.SystemParametersInfoW(20, 0, image , 0)

def set_wall(message):
    img = Image.new('RGB', auto.size(), color = (66, 70, 82))
    fnt = ImageFont.truetype('C:\\Windows\\Fonts\\consolab.ttf', 40)
    d = ImageDraw.Draw(img)
    d.text((250, 330), message, font=fnt, fill=(171, 220, 255))
    wall_path = 'E:\\pranil\\python\\jee_wallpaper.png'
    img.save(wall_path)
    set_background(wall_path)

while True:
    if datetime.today().second == 0:
        message = calculate_rem_time()  
        set_wall(message)

我得到的错误是:

OSError: [Errno 22] 无效参数:'E:\pranil\python\jee_wallpaper.png'

如何解决这个错误?

编辑: 我解决了这个问题..我只是在 img.save(wall_path) 和 set_background(wall_path) 之间添加了 time.sleep(1)

【问题讨论】:

    标签: python python-3.x windows automation oserror


    【解决方案1】:

    将 wall_path 更改为

    wall_path = 'E:\pranil\python\jee_wallpaper.png'

    【讨论】:

    • 错了,必须将字符串里面的反斜杠转义。
    • 或者你可以使用 r 字符串
    【解决方案2】:

    OSErrors 通常是由系统相关错误引起的(例如,文件未找到、磁盘已满等)。在您的情况下,您的文件路径看起来错误。尝试通过右键单击文件然后选择属性来复制和粘贴图像文件的文件路径。复制并粘贴 Location: C:.... 中列出的文件路径

    https://docs.python.org/3/library/exceptions.html

    https://docs.microsoft.com/en-us/dotnet/standard/io/file-path-formats

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-28
      • 2015-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多