我使用以下metohd并发读取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如果看到一个特定的错误字符串,我希望检查一个字符串的输出,并设置一个变量。在出现错误时,我将一个变量设置为以后使用,但是这个变量会丢失它的值。我想在我的代码的进一步部分中使用这个值。为什么变量会失去它的值?
发布于 2015-12-16 16:18:11
函数内部的FoundError是一个局部变量,它与外部作用域的FoundError无关。
相反,从函数返回标志:
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)https://stackoverflow.com/questions/34315770
复制相似问题