我正在尝试在我的python脚本中获取以下shell命令的输出,
hadoop fs -ls /projectpath/ | grep ^d | grep -v done | head -1 | awk {'print $8'}
我可以通过os.popen
成功获取输出,如下所示:
import os
cmd = "hadoop fs -ls /projectpath/ | grep ^d | grep -v done | head -1 | awk {'print $8'}"
p = os.popen(cmd,"r")
while 1:
line = p.readline()
if not line: break
print line
但是自{python 2.6}以来已弃用os.popen()
所以我想用subprocess.Popen()
函数替换上面的代码段。
但下面subprocess.Popen()
的代码段提供的结果与上面的代码段不同。
import subprocess as sub
import shlex
cmd = "hadoop fs -ls /projectpath/ | grep ^d | grep -v done | head -1 | awk {'print $8'}"
args = shlex.split(cmd)
p = sub.Popen(args,stdout=sub.PIPE,stderr=sub.PIPE)
output, errors = p.communicate()
print output
上面的命令只给出命令'hadoop fs -ls /projectpath/'
部分的输出。
我曾尝试为subpocess.Popen()
查询多个引用(http://docs.python.org/2/library/subprocess.html#popen-objects,Python, os.system for command-line call (linux) not returning what it should?),但无法在字符串cmd中执行该命令。谁能指出我做错了什么?
答案 0 :(得分:2)
试试这个:
cmd = "hadoop fs -ls /projectpath/ | grep ^d | grep -v done | head -1 | awk {'print $8'}"
p = sub.Popen(cmd,stdout=sub.PIPE,stderr=sub.PIPE, shell=True)