蟒子在命令行解析错误(python subprocess Command Line Parsing

2019-10-28 20:48发布

我跑我有一个子WolfPsort程序编码的Python程序。 它是蛋白定位检测程序的生物信息学工具。 然而,蟒蛇子不EXCUTE我输入文件。 这是代码

#!/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文件和“>”是必需的写入临时结果文件。

我怎样才能让子明白的说法“<”?

请帮助我!

Answer 1:

我猜你想使用shell魔术从文件名读取和写入tmpWolfResult。 为了做到这一点,你需要:

progWolf = subprocess.Popen('runWolfPsortSummary fungi < %s > tmpWolfResult'%filename, shell=True)

我觉得有义务提的是,由于该输入是从命令行参数来,它在技术上不是安全/信任和访问一个恶意用户可以在系统上运行该脚本可以做一些讨厌的东西。

但是,它可能更容易,你要发布这个脚本(或者只是自己使用它),你和你的用户或许不感兴趣,搞乱了自己的系统...



Answer 2:

<>通常由shell解释Popen()默认不不必要地产卵。 你可以使用stdinstdout参数而不是输入/输出重定向自/至文件:

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)

注: check_call()如果抛出一个异常runWolfPsortSummary非零状态退出。



Answer 3:

Popen功能需要用逗号分隔的参数列表。 你的方式写的,

'<'
filename
'>'

被作为三个独立的参数。 我假设你想连接到这一个参数。

progWolf = subprocess.Popen(['runWolfPsortSummary','fungi','<' + filename + '>','tmpWolfResult'])


文章来源: python subprocess Command Line Parsing Error