os.system() 不生成输出的问题

issues with os.system() not generating output

当我 运行 程序时,我得到了 q 之前的所有内容,但应该由 q 创建的 .mafft 返回为空。编辑:我忘了问实际问题。我在这里做错了什么?是我不知道的语法吗?

#!/usr/bin/python
import sys
import os
import math

data = sys.argv[2]
b = sys.argv[1]

bfile = open(b, "r")
for barcode in bfile:
        barcode = barcode.strip()
        print "barcode: %s" %barcode
        outfname = "%s.%s.fasta" % (data, barcode)
        print outfname
        outf = open(outfname,"w")
        handle = open(data, "r")
        for line in handle:
                linearr = line.split()
                sid = linearr[0]
                seq = linearr[1]
                potential_barcode = seq[0:len(barcode)]
                if potential_barcode == barcode:
                        outseq = line.replace(potential_barcode, "", 1)
                        newseq = outseq.split(' ',1)[-1].strip()
                        sys.stdout.write(newseq)
                        outf.write(">%s\n%s\n" % (sid,newseq))
        gamma = outfname + ".mafft"
        delta = gamma + ".stock"

        q =  "mafft %s > %s" % (outfname, gamma)
        os.system(q)

        qq = "fasta_to_stockholm %s > %s" % (gamma, delta)
        os.system(qq)

        qqq = "quicktree -out m %s" % (delta)
        os.system(qqq)

        handle.close()
        outf.close()
bfile.close()

这与使用os.system()而不是subprocess无关。 问题是您没有在 运行 程序之前关闭 outfname 文件。将关闭移动到 之前 os.system() 调用:

    handle.close()
    outf.close()

    gamma = outfname + ".mafft"
    delta = gamma + ".stock"

    q =  "mafft %s > %s" % (outfname, gamma)
    os.system(q)

mafft (outfname) 的输入文件是空的,因为缓冲区还没有被刷新,你是 "suffering from buffering"。

我会考虑每个人对使用 subprocess 的看法,但是 之后,您就可以使用它了!一次只改变一件事!