将命令的输出存储在变量中
Storing output of command in a variable
我正在使用 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 变量的值。请帮帮我。提前致谢。
假设您正在使用 subprocess
模块,如果您想与输出交互,最好使用 Popen
对象。
from subprocess import Popen, PIPE
proc = Popen(["halSummarizeMutations", hal_output], stdout=PIPE)
summary_mutation = proc.communicate()[0]
现在 summary_mutation
应该是子进程标准输出流内容的字符串表示形式。
我正在使用 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 变量的值。请帮帮我。提前致谢。
假设您正在使用 subprocess
模块,如果您想与输出交互,最好使用 Popen
对象。
from subprocess import Popen, PIPE
proc = Popen(["halSummarizeMutations", hal_output], stdout=PIPE)
summary_mutation = proc.communicate()[0]
现在 summary_mutation
应该是子进程标准输出流内容的字符串表示形式。