检查 python 命令的输出

2024-02-24

我有一个 python 脚本,它尝试运行外部命令并查找命令的结果。它需要使用外部命令输出中的值“count=”

COUNT_EXP = re.compile("count=(.*)")
cmd = [] # external command
p = subprocess.Popen(cmd,
                       stdout=subprocess.PIPE,
                       stderr=subprocess.STDOUT)
      for line in iter(p.stdout.readline, b''):
          result = COUNT_EXP.match(line)
          if result:
              print "count= " + result.group(1)
              return int(result.group(1))

当我尝试运行我的脚本时,我的外部命令(“cmd”)得到执行,我在 shell 中看到 count=10。但为什么我的 python 找不到它并在上面的“if”子句中打印出“count= 10?”?


p = subprocess.Popen(['python','blah.py'],stdout=subprocess.PIPE)
while True:
  line = proc.stdout.readline()
  if len(line) != 0:
    print "success"  #if this code works, expanding to your regex should also work
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

检查 python 命令的输出 的相关文章

随机推荐