我正在运行一个python程序,该程序是我用子进程WolfPsort program编写的。它是一个用于蛋白质定位检测程序的生物信息学工具。但是,python子进程不会执行我的输入文件。这是代码
#!/usr/bin/python
# secref.py is for secretome refining
import os
import sys
import subprocess
if len(sys.argv) != 2:
print >> sys.stderr, 'Usage: python secref.py [*.fasta]'
exit(1)
if sys.argv[1].endswith('.fasta'):
filename = sys.argv[1]
else:
print >> sys.stderr, 'Input correct file... [*.fasta]'
exit(1)
filehandle = open(filename,'r')
progWolf = subprocess.Popen(['runWolfPsortSummary','fungi','<',filename,'>','tmpWolfResult'])
progWolf.wait()
如果我运行代码,它会给出这样的错误消息:
[karyo@hostname secref.1.0]$ python secref.py A.carbonarius.fasta
Command Line Parsing Error; Do not know what to do with argument "<"
Usage:
runWolfPsortSummary [*OPTIONS*] *organismType*
runWolfPsortSummary (--usage|--help|--man)
Pipe sequences in from standard in.
子流程不识别"<"符号,但WolfPsort程序需要"<"来识别输入fasta文件,并且需要">"来写入临时结果文件。
如何使子流程理解参数"<"?
求你了,帮帮我!
我猜您正试图使用shell魔术来读取文件名并写入tmpWolfResult。为了实现这一点,您需要:
progWolf = subprocess.Popen('runWolfPsortSummary fungi < %s > tmpWolfResult'%filename, shell=True)
我觉得有义务提到,由于此输入来自命令行参数,因此从技术上讲,它不安全/不可信,并且有权在您的系统上运行此脚本的恶意用户可能会做一些令人讨厌的事情。
然而,更有可能的是,你正在分发这个脚本(或者只是自己使用它),而你和你的用户可能对破坏你自己的系统不感兴趣。。。
<
、>
通常由shell解释为默认情况下Popen()
不会不必要地生成。您可以使用stdin
、stdout
参数将输入/输出从/重定向到文件:
from subprocess import check_call
with open(filename) as file, open('tmpWolfResult', 'w') as output_file:
check_call(['runWolfPsortSummary', 'fungi'], stdin=file, stdout=output_file)
注意:如果runWolfPsortSummary
以非零状态退出,则check_call()
会引发异常。
Popen
函数采用一个用逗号分隔的参数列表。你写的方式,
'<'
filename
'>'
作为三个独立的参数发送。我假设你想把它连接成一个论点。
progWolf = subprocess.Popen(['runWolfPsortSummary','fungi','<' + filename + '>','tmpWolfResult'])