回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我有一个我想用的脚本subprocess.call执行一系列shell命令,但在执行时似乎忽略了一些命令。在</p>
<p>具体来说:</p>
<pre><code>#!/usr/bin/python
import tempfile
import subprocess
import os
import re
grepfd, grepfpath = tempfile.mkstemp(suffix=".xx")
sedfd, sedfpath = tempfile.mkstemp(suffix=".xx")
# grepoutfile = open( grepfpath, 'w')
sedoutfile = open( sedfpath, 'w' )
subprocess.call(['cp','/Users/bobby/Downloads/sample.txt', grepfpath])
sedcmd = [ 'sort',
grepfpath,
'|',
'uniq',
'|',
'sed',
'-e',
'"s/bigstring of word/ smaller /"',
'|',
'column',
'-t',
'-s',
'"=>"' ]
print "sedcmd = ", sedcmd
subprocess.call( ['ls', grepfpath ] )
subprocess.call( ['sort', '|', 'uniq' ], stdin = grepfd )
subprocess.call( sedcmd, stdout = sedoutfile )
</code></pre>
<p>它会生成以下输出:</p>
<p>Pythond3.py</p>
<p>sedcmd=['sort',/var/folders/3h/\u 0xwt5bx0hx8tgx06cmq9h_4f183ql/T/tmp5Gp0ff.xx','|','uniq','|','sed','-e','“s/bigstring of word/smaller/”,''124;','列','-t','-s','=>;'']
/var/folders/3h/_0xwt5bx0hx8tgx06cmq9h_4f183ql/T/tmp5Gp0ff.xx
排序:打开失败:|:没有这样的文件或目录<br/>
排序:无效选项--e
有关详细信息,请尝试“sort--help”。在</p>
<p>第一个'sort:open失败:|:没有这样的文件。。。来自第一个子进程调用['sort','|','uniq'],stdin=grepfd)
'sort:无效选项--e。。来自第二个子进程调用(sedcmd)。在</p>
<p>我见过很多在这种情况下使用管道的例子——那么我做错了什么呢?<br/>
谢谢!在</p>