Fetching data from command line output using python

697 Views Asked by At

Here is the command I used to get the file info, in which am interested in only revision version. command:

 command='si revisioninfo D:/Documentation/file_folder/file.c'
 process = Popen(args=command,stdout=PIPE,shell=True)
 file_output=process.communicate()[0]
 print file_output

output:

Sandbox Name: D:/Documentation/project.pj
Revision: 1.7
Labels: Review_1

Out of these I want just the revision data to be assigned to the output.

1

There are 1 best solutions below

0
spejsy On

If the revision part is always the second line of the output you could use splitlines() to split the string by newline:

file_output = process.communicate()[0].splitlines()[1]

If that is not the case you could always get the first line that starts with 'Revision:':

file_output = [line for line in process.communicate()[0].splitlines() if line.startswith('Revision:')][0]