如何通过Python来调用/运行软件?

2024-06-17 08:11:29 发布

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

我正在尝试制作一个简单的脚本来逐个(一个接一个)运行不同的任务,但我不知道如何用python脚本运行程序!我知道应该很简单!但我在哪儿都找不到。我的例子是:

samtools merge filename.bam file1.sorted.bam file2.sorted.bam file3.sorted.bam file4.sorted.bam

samtools index filename.bam 
samtools idxstats filename.bam > filename.txt 
samtools pileup -vcf path/filename.fa filename_sorted.bam

我希望python运行第一个命令,在它完成之后,再转到下一个命令!重要的是要等到它完成!在


Tags: 命令程序脚本indexmergefilenamefile1例子
2条回答
from subprocess import call # call runs an external program and waits for it to quit

for command in ("samtools merge filename.bam file1.sorted.bam file2.sorted.bam file3.sorted.bam file4.sorted.bam", 
                "samtools index filename.bam", 
                "samtools idxstats filename.bam > filename.txt", 
                "samtools pileup -vcf path/filename.fa filename_sorted.bam"):
    # shell=True is so you can handle redirects like in the 3rd command
    call(command, shell=True) 

使用subprocess module。页面底部有很多例子。在

相关问题 更多 >