Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python中多个模块的错误处理_Python_Error Handling_Python Module - Fatal编程技术网

python中多个模块的错误处理

python中多个模块的错误处理,python,error-handling,python-module,Python,Error Handling,Python Module,我有一个项目,有多个不同的模块 project/ read_email.py browse.py vsw.py mdf.py cill.py utils_funcs.py 所以read_email.py读取一封电子邮件,然后调用vsw.py import win32com.client from vsw import vsw def read_email(): outlook = win32com.client.Dispatch("Ou

我有一个项目,有多个不同的模块

project/
    read_email.py
    browse.py
    vsw.py
    mdf.py
    cill.py
    utils_funcs.py
所以
read_email.py
读取一封电子邮件,然后调用
vsw.py

import win32com.client
from vsw import vsw
def read_email():

    outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
    pc_fold_struct = os.environ['USERPROFILE'] + '\\Documents\\project\\'
    inbox = outlook.GetDefaultFolder(6)

    messages = inbox.Items

    for message in messages:
        if message.Unread:
            vsw(pc_fold_struct)
vsw
中,我从其余模块调用其他函数

从utils\u funcs导入中单击\u项 从mdf导入mdf 从浏览导入浏览 从cill导入cill 导入操作系统 def在vsw(pc折叠结构)时运行其他进程:

下面是我的
mdf
功能的一小部分

 from utils_funcs import click_item
#mdf
 def mdf():
    #also here how to make it go back to read_email if `click_item` count is 20
    click_item(1, 1, "my_fav")
    click_item(1, 1, "mdf")
    #other code occurs here.
除了
utils\u funcs.py
。此模块的独特之处在于我有一个功能
单击项目
(如下)

它使用
pyautogui
单击图像。但是我在其他模块中都使用了这个函数。由于它是一个连续循环,如果陷入错误,我希望它在计数大于20时中断循环,然后返回
read_email.py
模块。我能想到的唯一方法是从循环中返回一些东西,并检查每个模块,如果它是这个变量,如果它退出程序。例如,我的函数将变成

def click_item(time_one, time_two, img, **kwargs):

    dir_name = 'c:\\'
    coords = None
    count = 1
    while coords == None:
        try:
            time.sleep(time_one)
            coords = pyautogui.locateCenterOnScreen(dir_name + "img\\" + img + ".png", grayscale=False)
            pyautogui.moveTo(x=coords.x, y=coords.y)
            pyautogui.click()
            time.sleep(time_two)
        except Exception as e:
            if count == 20:
                return True
            else:
                count += 1
                coords = None
但不确定如何在其他模块中处理此问题,以使其返回到
read_email.py


我之所以不让进程中断,是因为我正在使用
pyautogui
自动化的程序有时需要时间来加载映像。这就是为什么在
上的
单击_项
功能中,除了
I set
coords=None
之外,如果它在20次之后没有找到图像。那么这是一个错误,我需要返回阅读电子邮件。

您应该引发异常并在caller@juanpa.arrivillaga例如,你是说在每个调用
click\u item
的模块中,除了围绕它之外,都要尝试一次吗?如果关键是让它失败,那么你就不能处理这个错误。然后程序将终止问题是,有时它需要等待图像出现,但一旦超过20-30次,就会出现错误,我想回去检查电子邮件。如果这有道理的话,那就没有道理了。你需要提供一个新的解决方案。“read_email.py读取电子邮件,然后调用vsw.py”到底是什么意思?您应该引发异常并在caller@juanpa.arrivillaga例如,你是说在每个调用
click\u item
的模块中,除了围绕它之外,都要尝试一次吗?如果关键是让它失败,那么你就不能处理这个错误。然后程序将终止问题是,有时它需要等待图像出现,但一旦超过20-30次,就会出现错误,我想回去检查电子邮件。如果这有道理的话,那就没有道理了。你需要提供一个新的解决方案。“read_email.py读取电子邮件,然后调用vsw.py”到底是什么意思?
def click_item(time_one, time_two, img, **kwargs):

    dir_name = 'c:\\'
    coords = None
    while coords == None:
        try:
            time.sleep(time_one)
            coords = pyautogui.locateCenterOnScreen(dir_name + "img\\" + img + ".png", grayscale=False)
            pyautogui.moveTo(x=coords.x, y=coords.y)
            pyautogui.click()
            time.sleep(time_two)
        except Exception as e:
            coords = None
def click_item(time_one, time_two, img, **kwargs):

    dir_name = 'c:\\'
    coords = None
    count = 1
    while coords == None:
        try:
            time.sleep(time_one)
            coords = pyautogui.locateCenterOnScreen(dir_name + "img\\" + img + ".png", grayscale=False)
            pyautogui.moveTo(x=coords.x, y=coords.y)
            pyautogui.click()
            time.sleep(time_two)
        except Exception as e:
            if count == 20:
                return True
            else:
                count += 1
                coords = None