【发布时间】:2013-08-17 03:46:14
【问题描述】:
我有一个 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?"?
【问题讨论】:
-
您的 if 子句可能评估为 False,因此您永远不会打印计数。也许您的 python 工作目录不包含该文件。
标签: python