【发布时间】:2013-03-02 08:42:51
【问题描述】:
在 shell 中执行它会得到切实的结果:
wget -O c1 --no-cache "http://some.website" | sed "1,259d" c1 | sed "4,2002d"
在 Python 中执行此操作对我没有任何帮助:
subprocess.call(shlex.split("wget -O c1 --no-cache \"http://some.website/tofile\""))
c1 = open("c1",'w')
first = subprocess.Popen(shlex.split("sed \"1,259d\" c1"), stdout=subprocess.PIPE)
subprocess.Popen(shlex.split("sed \"4,2002d\""), stdin=first.stdout, stdout=c1)
c1.close()
这样做也没有结果:
c1.write(subprocess.Popen(shlex.split("sed \"4,2002d\""), stdin=first.stdout, stdout=subprocess.PIPE).communicate()[0])
“让我一无所获”是指文件中的空白输出。有没有人看到这里有什么不寻常的地方?
【问题讨论】:
-
你在 Python 命令行解释器中试过这个吗?在此过程中的每个步骤中,各种变量显示了什么?
-
它们是 subprocess.Popen 对象。
c1当然只是一个文件对象。
标签: python shell unix subprocess pipe