在变量中存储命令的输出

时间:2015-01-12 11:26:37

标签: python subprocess

我正在使用Python脚本中的call()函数从外部软件中执行命令。我想将函数的输出存储到变量中并使用变量。但是,输出值根本不会存储到变量中。      当我给命令打印变量时,它打印0这是默认值。代码是:

from subprocess inport call
print "\n Summarizing mutations"
summary_mutation = call(["halSummarizeMutations", hal_output])
print summary_mutation

hal_output是命令halSummarizeMutations的输入文件。我得到的输出是:

GenomeName, ParentName, BranchLength, GenomeLength, ParentLength, Subtitutions, Transitions, Transversions, Matches, GapInsertions, GapInsertedBases, GapDeletions, GapDeletedBases, Insertions, InsertionBases, Deletions, DeletionBases, Inversions, InvertedBases, Duplications, DuplicatedBases, Transpositions, TranspositionBases, Other
ancestral_sequences, homo_sapiens, 1, 225206, 248956422, 149218, 49494, 99724, 49433, 10, 698, 0, 0, 17, 20539, 10, 4345, 1343, 134907, 672, 57699, 0, 0, 0
gorilla_gorilla, homo_sapiens, 1, 229507203, 248956422, 44226, 13434, 26644, 15224, 68, 593, 120, 6319, 15, 229443572, 6, 857, 0, 0, 0, 0, 0, 0, 10
papio_anubis, homo_sapiens, 1, 220367699, 248956422, 98187, 32113, 64759, 32284, 6, 1418, 11, 245, 36, 220231921, 30, 7448, 791, 109496, 0, 0, 0, 0, 0
Total, ,3, 450100108, 746869266, 291631, 95041, 191127, 96941, 84, 2709, 131, 6564, 68, 449696032, 46, 12650, 2134, 244403, 672, 57699, 0, 0, 0
Average, ,1, 150033369, 248956422, 97210, 31680, 63709, 32313, 28, 903, 43, 2188, 22, 149898677, 15, 4216, 711, 81467, 224, 19233, 0, 0, 0

0

其中0是summary_mutation变量的值。请帮帮我。提前谢谢。

1 个答案:

答案 0 :(得分:1)

假设您正在使用subprocess模块,如果您想与输出进行交互,最好使用Popen个对象。

from subprocess import Popen, PIPE

proc = Popen(["halSummarizeMutations", hal_output], stdout=PIPE)
summary_mutation = proc.communicate()[0]

现在summary_mutation应该是子进程内容的字符串表示形式。标准输出流。