2016-08-29 56 views
0

我试图通过Python代码执行shell命令,但我无法理解它为什么失败。使用子进程执行时出错

当打印命令并将其粘贴到shell以尝试直接执行它时效果很好,这是奇怪的部分。

从Python中,我发现了以下内容:

/bin/sh: -c: line 0: syntax error near unexpected token `(' 
/bin/sh: -c: line 0: `/DATA/NGS/ngs_software/bioinfoSoftware/bwa_current/bwa mem ... --threads 4 -T /tmp/samTemp -' 

有什么我失踪?我的代码看起来像这样,其中'cmd'是带有命令的字符串。该操作系统是一个CentOS用的bash shell:

process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE) 
out = process.stdout.readline() 
out = out.decode("utf-8").strip('\n') 
+0

什么是命令? – Samuel

+0

'shell尝试直接执行它'完全可以正常工作'空闲保存你的应用程序,但是'谁在外面保存你的应用程序? – dsgdfg

+0

@chepner给了我正确的答案,这是一个不能正常工作的替换。感谢您的帮助! –

回答

0

你的命令包含进程替换,但Popen运行使用/bin/sh它的命令。当以/bin/sh运行时,bash不允许进程替换。您可以使用executable选项明确请求使用bash运行该命令。

process = subprocess.Popen(cmd, shell=True, executable='/bin/bash', stdout=subprocess.PIPE) 
+0

太棒了,这确实有用,不知道。非常感谢你的帮助:) –