首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >退出python管道和设置var

退出python管道和设置var
EN

Stack Overflow用户
提问于 2015-12-16 15:22:34
回答 1查看 20关注 0票数 1

我使用以下metohd并发读取python脚本的输出并将其写入文件:

代码语言:javascript
复制
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

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-12-16 16:18:11

函数内部的FoundError是一个局部变量,它与外部作用域的FoundError无关。

相反,从函数返回标志:

代码语言:javascript
复制
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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34315770

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档