在外部程序调用中插入变量(子流程.call/Popen)在Python中

2024-10-02 20:41:43 发布

您现在位置: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。嗯测试.fasta作为输入文件。在

不过,如果我试图用变量outfilehmmprofilefastafile我打算接收这些变量作为Python脚本的参数,并使用它们来构建外部程序调用

^{pr2}$

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'])

因此,我想我需要了解将变量插值到调用中要遵循的过程,因为问题似乎就在那里。在

你们谁能帮我解决这个问题吗?在

提前谢谢


Tags: 程序脚本scriptscalloutexefastasubprocess
2条回答

尝试改变这一点:

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

^{pr2}$

编辑

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

你有:

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

但这不是传递变量outfile。它传递一个字符串'outfile'。在

你想要:

^{pr2}$

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

相关问题 更多 >