当我在解释器中运行以下命令时
infile_intersect = subprocess.Popen(['cut', '-f', '1,2,3,4,5', infile, r'|', 'intersectBed', '-a', 'stdin', '-b', bound_motif, '-wo', r'|', 'sort', '-k', '1,1', '-k', '2,2n', '|uniq'], stdout=subprocess.PIPE).communicate()
我收到错误 cut: invalid option -- a 但是当我对列表进行空格连接时它似乎没问题
>>> ' '.join(['cut', '-f', '1,2,3,4,5', infile, r'|', 'intersectBed', '-a', 'stdin', '-b', bound_motif, '-wo', r'|', 'sort', '-k', '1,1', '-k', '2,2n', '|uniq'])
'cut -f 1,2,3,4,5 test.bed | intersectBed -a stdin -b ENCODE.tf.bound.union.bed -wo | sort -k 1,1 -k 2,2n |uniq'
貌似管道没有正确发出,但我不确定为什么
请您参考如下方法:
这不是您使用子流程进行管道传输的方式,将 | 与列表输入表单一起使用是无效的。您应该将完整的字符串传递给它并使用 shell=True 或像我在下面的示例中所做的那样使用管道:
>>> import subprocess
>>> p = subprocess.Popen(['cat', 'abc1'], stdout=subprocess.PIPE)
>>> p1 = subprocess.Popen(['uniq', '-c'], stdin=p.stdout, stdout=subprocess.PIPE)
>>> print p1.communicate()[0]
3 word1
1 word3
1 word4
1 word5
使用字符串和 shell=True
>>> print subprocess.Popen('cat abc1 | uniq -c', shell=True,
stdout=subprocess.PIPE).communicate()[0]
3 word1
1 word3
1 word4
1 word5
来自 docs :
Warning Invoking the system shell with
shell=Truecan be a security hazard if combined with untrusted input.
