views:

229

answers:

4

I have been working on this particular assignment and ran into one issue. I'm fairly new to python and needed a quick fix. 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 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

cmd = ' -a ' + str(a) + ' -b ' + str(b) works fine but it's the last one that doesn't. I do understand that you have to define something before you use it later on (in the example giving me the error that 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 of course and it's just a dance over and over moving things before the other and I can't seem to figure it out. str(Output) should be what is printed on line 6 when the .exe is ran.

Thanks for all the help. I hope I was descriptive enough in my quesiton. Showing what you would do would be helpful also.

A: 

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.

contagious
This will not work, the call to Popen requires cmd.
AlbertoPL
You missed `Popen(cmd, ...`.
avakar
+5  A: 

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.

Welbog
Thank you! :) that was very helpful. also thanks everyone for input!!
If so, accept the answer :-)
Roberto Liffredo
+1  A: 

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]
S.Lott
+1  A: 

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)

lorn00