在Python

时间:2015-10-21 10:57:19

标签: python

我正在尝试从Python脚本运行外部程序。

在这里搜索和阅读多篇文章后,我找到了似乎是解决方案。

首先,我使用了 subprocess.call 函数。

如果我以这种方式构建命令:

hmmer1=subprocess.call("D:\Python_Scripts\HMMer3\hmmsearch.exe --tblout hmmTestTab.out SDHA.hmm Test.fasta")

外部程序 D:\ Python_Scripts \ HMMer3 \ hmmsearch.exe hmmTestTab.out 作为输出的文件名运行, SDHA.hmm < / strong>和 Test.fasta 作为输入文件。

然而,如果我尝试用变量 outfile hmmprofile fastafile 替换文件名(我打算收到这些变量作为Python脚本的参数,并使用它们来构建外部程序调用),

hmmer2=subprocess.call("D:\Python_Scripts\HMMer3\hmmsearch.exe --tblout outfile hmmprofile fastafile")

Python打印出一个无法打开输入文件的错误。

我还使用了“Popen”功能,结果类似:

此通话有效

hmmer3=Popen(['D:\Python_Scripts\HMMer3\hmmsearch.exe', '--tblout','hmmTestTab.out', 'SDHA.hmm','Test.fasta'])

而且这个不是

hmmer4=Popen(['D:\Python_Scripts\HMMer3\hmmsearch.exe', '--tblout','outfile', 'hmmprofile','fastafile'])

由于这个原因,我认为我需要了解将变量插入到调用中的过程,因为看起来问题就在那里。

你们有没有人帮我解决这个问题?

提前致谢

2 个答案:

答案 0 :(得分:0)

尝试改变这一点:

hmmer1=subprocess.call("D:\Python_Scripts\HMMer3\hmmsearch.exe" 

hmmer1=subprocess.call('D:\\Python_Scripts\\HMMer3\\hmmsearch.exe'

修改

argv = ' --tblout outfile hmmprofile fastafile' # your arguments
program = [r'"D:\\Python_Scripts\\HMMer3\\hmmsearch.exe"', argv]
subprocess.call(program) 

答案 1 :(得分:0)

你有:

hmmer4=Popen(['D:\Python_Scripts\HMMer3\hmmsearch.exe', '--tblout','outfile', 'hmmprofile','fastafile'])

但是没有传递变量 outfile。它传递字符串'outfile'

你想:

hmmer4=Popen(['D:\Python_Scripts\HMMer3\hmmsearch.exe', '--tblout', outfile, hmmprofile, fastafile])

另一个答案是正确的,尽管它解决了另一个问题;你应该加倍反斜杠,或使用r''原始字符串。