3

When this .exe file runs it prints a screen full of information and I want to print a particular line out to the screen, here on 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 fine: cmd = ' -a ' + str(a) + ' -b ' + str(b)

This doesn't work: cmd = ' -a ' + str(a) + ' -b ' + str(b) + str(Output)

I get an error saying Output isn't defined. But when I cut and paste:

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

before the cmd statement it tells me the process isn't defined. str(Output) should be what is printed on line 6 when the .exe is ran.

4 Answers 4

5

You're trying to append the result of a call into the call itself. You have to run the command once without the + str(Output) part to get the output in the first place.

Think about it this way. Let's say I was adding some numbers together.

 z = 5 + b
 b = z + 2

I have to define either z or b before the statements, depending on the order of the two statements. I can't use a variable before I know what it is. You're doing the same thing, using the Output variable before you define it.

Sign up to request clarification or add additional context in comments.

Comments

1

It's not supposed to be a "dance" to move things around. It's a matter of what's on the left side of the "=". If it's on the left side, it's getting created; if it's on the right side it's being used.

As it is, your example can't work even a little bit because line one wants part of output, which isn't created until the end.

The easiest way to understand this is to work backwards. You want to see as the final result?

print output[5]

Right? So to get there, you have to get this from a larger string, right?

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

So where did outputstring come from? It was from some subprocess.

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

So where did process come from? It was created by subprocess Popen

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

So where did cmd come from? I can't tell. Your example doesn't make sense on what command is being executed.

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

Comments

1

Just change your first line to:

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

and the print statement at the end to:

print cmd + str(Output)

This is without knowing exactly what it is you want to print... It -seems- as if your problem is trying to use Output before you actually define what the Output variable is (as the posts above)

Comments

0

Like you said, a variable has to be declared before you can use it. Therefore when you call str(Output) ABOVE Output = outputlist[5], Output doesn't exist yet. You need the actually call first:

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

then you can print the output of that command:

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

should be the line directly above print cmd_return.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.