退出 python 管道并设置变量

Exiting python Pipe and setting var

我正在使用以下方法同时读取我的 python 脚本的输出并将其写入文件:

FoundError = 0    
def execute(command):    
  with Popen(command, stdout=PIPE, bufsize=1, universal_newlines=True) as p:
    for line in p.stdout:
        print(line, end='',flush=True)
        if 'Error found in job. Going to next' in line:
            FoundError = 1
            break
execute(myCmd)
print(FoundError) --->>this gives a 0 even if I see an error

我想检查字符串的输出并在看到特定错误字符串时设置一个变量。出现错误时,我设置了一个变量供以后使用,但是这个变量失去了它的值。我想在代码的后续部分使用这个值。 为什么变量丢失了它的值?

函数内部的

FoundError 是一个局部变量,与外部范围的 FoundError 无关。

Return 函数中的标志改为:

def find_error(command):
    ...
    if 'Error found in job. Going to next' in line:
        return True # found error

found_error = find_error(command)
print(found_error)