问题
I am running a python program I have coded with a subprocess WolfPsort Program. It is a bioinformatics tool for protein localization detection program. However, the python subprocess does not excute my input file. This is the code
#!/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()
If I run the code it gives error message like this:
[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.
The subprocess dose not recognise "<" symbol, but WolfPsort Program needs "<" to recognise the input fasta file and ">" is required to write a temporary result file.
How can I make the subprocess to understand the argument "<"?
Please, help me out!
回答1:
I'm guessing you're trying to use shell magic to read from filename and write to tmpWolfResult. In order to make that happen, you need:
progWolf = subprocess.Popen('runWolfPsortSummary fungi < %s > tmpWolfResult'%filename, shell=True)
I feel obligated to mention that since this input is coming from a commandline argument, it's technically not safe/trusted and a malicious user with access to run this script on your system could do some nasty things.
However, it's probably more likely that you're distributing this script (or just using it yourself) and you and your users probably aren't interested in messing up your own system...
回答2:
<
, >
are usually interpreted by the shell that Popen()
doesn't spawn unnecessarily by default. You could use stdin
, stdout
parameters instead to redirect input/output from/to files:
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)
Note: check_call()
raises an exception if runWolfPsortSummary
exits with non-zero status.
回答3:
The Popen
function takes an argument list separated by commas. The way you wrote it,
'<'
filename
'>'
is sent as three separate arguments. I'm assuming you want to concatenate that into one argument.
progWolf = subprocess.Popen(['runWolfPsortSummary','fungi','<' + filename + '>','tmpWolfResult'])
来源:https://stackoverflow.com/questions/23274329/python-subprocess-command-line-parsing-error