How to print output using python?

When this .exe file is launched, it displays the full text, and I want to print a specific line on the screen, here in line "6":

    cmd =  ' -a ' + str(a) + ' -b ' + str(b) + str(Output)
    process = Popen(cmd, shell=True, stderr=STDOUT, stdout=PIPE)
    outputstring = process.communicate()[0]
    outputlist = outputstring.splitlines()
    Output = outputlist[5]
    print cmd

This works great: cmd = ' -a ' + str(a) + ' -b ' + str(b)

This does not work: cmd = ' -a ' + str(a) + ' -b ' + str(b) + str(Output)

I get an Outputundefined error message . But when I cut and paste:

    outputstring = process.communicate()[0]
    outputlist = outputstring.splitlines()
    Output = outputlist[5]

before the cmd expression, it tells me that the process is not defined. str(Output)should be what is printed on line 6 when running .exe.

+3
source share
4 answers

You are trying to add the result of a call to the call itself. You must execute the command once without a part + str(Output)to get the result first.

Think of it this way. Say I added a few numbers.

 z = 5 + b
 b = z + 2

z, b , . , , . , Output, .

+5

"", . , "=". , ; .

, , , .

- . ?

print output[5]

? , , , ?

output= outputstring.splitlines()
print output[5]

outputstring? .

outputstring = process.communicate()[0]
output= outputstring.splitlines()
print output[5]

, ? Popen

process = Popen(cmd, shell=True, stderr=STDOUT, stdout=PIPE)
outputstring = process.communicate()[0]
output= outputstring.splitlines()
print output[5]

, cmd? . , .

cmd = ?  
process = Popen(cmd, shell=True, stderr=STDOUT, stdout=PIPE)
outputstring = process.communicate()[0]
output= outputstring.splitlines()
print output[5]
+1

:

cmd = '-a' + str (a) + '-b' + str (b)

:

print cmd + str ()

, ... , , Output, , Output ( )

+1

, , . , str(Output) Output = outputlist[5], . :

cmd =  ' -a ' + str(a) + ' -b ' + str(b)

:

cmd_return =  ' -a ' + str(a) + ' -b ' + str(b) + str(Output)

print cmd_return.

0

Source: https://habr.com/ru/post/1711938/


All Articles