Kratzdisteln 2024-08-06 18:21 采纳率: 75%
浏览 99
已结题

【pyautogui 报错】OSError: Failed to read X because file is missing, has improper permissions

报错:
OSError: Failed to read D:\STEFANOS\python oolsuto\icons\jupyter.png because file is missing, has improper permissions, or is an unsupported or invalid format

[ WARN:0@54.445] global loadsave.cpp:241 cv::findDecoder imread_('D:\STEFANOS\python oolsuto\icons\jupyter.png'): can't open/read file: check file path/integrity

程序:

import pyautogui 
#import pyperclip
import time
 
def open_jupyter():
    print('Opening jupyter...')
    time.sleep(1)
    
    try:
        location = pyautogui.locateOnScreen("D:\STEFANOS\python\tools\auto\icons\jupyter.png",confidence=0.5)
        print(location)
        pyautogui.click(location)
    except pyautogui.ImageNotFoundException:
        print("Sorry I cannot locate Jupyter")

    time.sleep(1)


open_jupyter()

  • 写回答

1条回答 默认 最新

  • 梦回阑珊 2024-08-06 20:04
    关注

    你遇到的错误 OSError: Failed to read ... because file is missing, has improper permissions, or is an unsupported or invalid format 表明程序无法读取指定路径的图像文件。以下是解决该问题的一些步骤:

    可能原因及解决方法:
    文件路径错误:

    确认文件路径是否正确。你在代码中的路径是 D:\STEFANOS\python oolsuto\icons\jupyter.png,但是注释中的路径是 D:\STEFANOS\python\tools\auto\icons\jupyter.png。请确认使用的路径是正确的。
    Windows 路径中反斜杠 () 需要转义,或者使用原始字符串(r"...")。例如:

    location = pyautogui.locateOnScreen(r"D:\STEFANOS\python\tools\auto\icons\jupyter.png", confidence=0.5)
    
    
    

    文件权限问题:

    确认你的程序有权限读取该文件。尝试手动打开该文件以检查是否存在权限问题。
    文件格式问题:

    确认文件是有效的 PNG 格式。尝试用图像查看器打开文件,确保文件不是损坏的。
    图像文件缺失:

    确认文件在指定路径下是否存在。你可以使用以下代码检查文件是否存在:

    import os
    file_path = r"D:\STEFANOS\python\tools\auto\icons\jupyter.png"
    if os.path.isfile(file_path):
        print("文件存在")
    else:
        print("文件不存在")
    
    
    

    代码示例
    根据以上的可能原因,你可以调整代码如下:

    import pyautogui
    import time
    import os
    
    def open_jupyter():
        print('Opening jupyter...')
        time.sleep(1)
        
        file_path = r"D:\STEFANOS\python\tools\auto\icons\jupyter.png"
        
        if os.path.isfile(file_path):
            try:
                location = pyautogui.locateOnScreen(file_path, confidence=0.5)
                if location:
                    print(location)
                    pyautogui.click(location)
                else:
                    print("Sorry, I cannot locate Jupyter")
            except pyautogui.ImageNotFoundException:
                print("Sorry, I cannot locate Jupyter")
            except OSError as e:
                print(f"OS error: {e}")
        else:
            print(f"文件 {file_path} 不存在")
        
        time.sleep(1)
    
    open_jupyter()
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 8月14日
  • 已采纳回答 8月6日
  • 创建了问题 8月6日

悬赏问题

  • ¥15 有可能用平板通过拓展坞来烧录程序吗(keil5的那种)
  • ¥15 网络分析设施点无法识别
  • ¥15 状态图的并发态问题咨询
  • ¥15 PFC3D,plot
  • ¥15 VAE模型编程报错无法解决
  • ¥100 基于SVM的信息粒化时序回归预测,有偿求解!
  • ¥15 物体组批优化问题-数学建模求解答
  • ¥350 麦克风声源定位坐标不准
  • ¥15 apifox与swagger使用
  • ¥15 egg异步请求返回404的问题