子流程调用运行m

2024-06-08 03:05:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我编写了一个脚本从终端运行mafft模块:

 import subprocess


def linsi_MSA(sequnces_file_path):
    cmd = ' mafft --maxiterate 1000 --localpair {seqs} > {out}'.format(seqs=sequnces_file_path, out=sequnces_file_path)
    subprocess.call(cmd.split(), shell=True)

if __name__ == '__main__':
    import logging
    logger = logging.getLogger('main')
    from sys import argv
    if len(argv) < 2:
        logger.error('Usage: MSA <sequnces_file_path> ')
        exit()
    else:
        linsi_MSA(*argv[1:])

由于某些原因,在尝试使用以下命令从终端运行脚本时:

python ./MSA.py ./sample.fa

我直接在终端打开mafft交互式版本(请求输入..输出等)

当我试图使用以下命令直接在终端中写入cmd时:

mafft --maxiterate 1000 --localpair sample.fa > sample.fa 

它按预期工作,在不打开交互版本的情况下执行命令行版本。你知道吗

我希望我的脚本能够在终端上执行命令行版本。有什么问题吗?你知道吗

谢谢!你知道吗


Tags: samplepathimport版本脚本cmd终端msa
1条回答
网友
1楼 · 发布于 2024-06-08 03:05:16

如果使用shell=True,则应传递一个字符串作为参数,而不是列表,例如:

subprocess.call("ls > outfile", shell=True)

文档中没有解释,但我怀疑这与底层库函数的最终调用有关:

call(["ls", "-l"])  > execlp("ls", "-l")

      ^^^^^^^^^^              ^^^^^^^^^^
call("ls -l", shell=True)  > execlp("sh", "-c", "ls -l")
     ^^^^^^^                                     ^^^^^^^ 

call(["ls", "-l"], shell=True)  > execlp("sh", "-c", "ls", "-l")

# which can be tried from command line:
sh -c ls -l
# result is a list of files without details, -l was ignored.
# see sh(1) man page for -c string syntax and what happens to further arguments.

相关问题 更多 >

    热门问题