tags:

views:

289

answers:

4

When an exe file is run it prints out some stuff. I'm trying to run this on some numbers below and print out line 54 ( = blah ). It says process isn't defined and I'm really unsure how to fix this and get what I want printed to the screen. If anyone could post some code or ways to fix this thank you so very much!

for j in ('90','52.62263','26.5651','10.8123'):
    if j == '90':
        k = ('0',)
    elif j == '52.62263':
        k = ('0', '72', '144', '216', '288')
    elif j == '26.5651':
        k = (' 324', ' 36', ' 108', ' 180', ' 252')
    else:
        k = (' 288', ' 0', ' 72', ' 144', ' 216')

    for b in k:

        outputstring = process.communicate()[0]
        outputlist = outputstring.splitlines()
        blah = outputlist[53]

        cmd =  ' -j ' + str(j) + ' -b ' + str(b) + ' blah '

        process = Popen(cmd, shell=True, stderr=STDOUT, stdout=PIPE)

        print cmd

I am trying to print out for example:

-j 90 -az 0 (then what blah contains) blah is line 54. Line 54 prints out a lot of information. Words mostly. I want to print out what line 54 says to the screen right after

-j 90 -az 0

@ Robbie: line 39

blah = outputlist[53]

Indexerror: list index out of range

@ Robbie again. Thanks for your help and sorry for the trouble guys...

I even tried putting in outputlist[2] and it gives same error :/

+2  A: 

Are you sure this is right

cmd =  ' -j ' + str(el) + ' -jk ' + str(az) + ' blah '

Where's your executable?

eduffy
well I'm not sure ' blah ' is right. Taking ' blah ' out is doing what I want it to do. Essentially it prints (without blah):-k 90 -jk 0-k 52.62253 -jk 0-k 52.62243 -jk 72etc etc etcExecutable is in program files a folder a folder then the .exe file
Tyler
-k 90 -jk 0 is it's own line
Tyler
and so on and so forth
Tyler
but how are you telling Popen where to find your .exe? It's not in cmd.
eduffy
exepath = os.path.join('EXE file location)exepath = '"' + os.path.normpath(exepath) + '"'print exepathI do that before the for statement mentioned here. How do I get it to print that?
Tyler
@Tyler: Please do not comment vaguely on your own question. Please update your question with complete facts. "and so on and so forth" in't helpful. How can we help you if you can't keep the question clear and complete?
S.Lott
exe path= os.path ...... exepath="' + os.path. ....... print exepath ...... are all separate lines. I'm sorry not sure how to show that on here with comments
Tyler
I'm commenting vaguely maybe cause I'm a beginner and unsure how to express myself...
Tyler
@Tyler: Don't answer in the comments. Fix your question. It's your question. Fix it, please.
S.Lott
@Tyler: Even beginners can provide details of what their file looks like, what their error messages are and what the current version of their script is.
S.Lott
+2  A: 

The following line

outputstring = process.communicate()[0]

calls the communicate() method of the process variable, but process has not been defined yet. You define it later in the code. You need to move that definition higher up.

Also, your variable names (j,k, and jk) are confusing.

Hank Gay
Though if that was the whole problem, that would just give an UnboundLocal exception.
balpha
ok edited it so it may be less confusing jk is now b
Tyler
Also, here's the other problem.... Like for instance I move the process definition right under the for b in k: and now it tells me that my cmd is not defined
Tyler
+1  A: 

process isn't defined because your statements are out of order.

    outputstring = process.communicate()[0]
    outputlist = outputstring.splitlines()
    blah = outputlist[53]

    cmd =  ' -j ' + str(j) + ' -b ' + str(b) + ' blah '

    process = Popen(cmd, shell=True, stderr=STDOUT, stdout=PIPE)

cannot possibly work. process on the first line, is undefined.

S.Lott
I move prcoess one line ahead of outputsring = process.communicate()[0] and it tells me cmd isn't defined
Tyler
Are you moving statements around at random? What do you hope to accomplish by that? if you've changed the code, please UPDATE your question.
S.Lott
Tyler, you'll have to move cmd up to.Things have to be defined before you use them, this little fact seems to be your hangup.
Robbie
I try that. right after the for az in z: i put the process = and cmd = statements and it says cmd isn't defined
Tyler
@Tyler: Order Matters. cmd= then process= . In that order.
S.Lott
az in z? Which process are you trying to communicate with? You have process.communicate, when process doesn't show up anywhere before that. But it appears that in some way you're trying to then use the output of it to construct some bizarre command for a new process. Surely you can see how this is confusing everyone.
Robbie
+6  A: 

I can't help but clean that up a little.

# aesthetically (so YMMV), I think the code would be better if it were ...
# (and I've asked some questions throughout)

j_map = {
  90: [0], # prefer lists [] to tuples (), I say...
  52.62263: [0,  72, 144, 216, 288],
  26.5651: [324, 36, 108, 180, 252],
  10.8123: [288,  0, 72, 144, 216]
   }
# have a look at dict() in http://docs.python.org/tutorial/datastructures.html
# to know what's going on here -- e.g. j_map['90'] is ['0',]

# then the following is cleaner
for j, k in j_map.iteritems():
  # first iteration j = '90', k=[0]
  # second iteration j = '52.62263'', k= [0,...,288]
  for b in k:
    # fixed the ordering of these statements so this may actually work
    cmd = "program_name -j %f -b %d" % (j, b)
      # where program_name is the program you're calling
      # be wary of the printf-style %f formatting and
      #     how program_name takes its input
    print cmd
    process = Popen(cmd, shell=True, stderr=STDOUT, stdout=PIPE)
    outputstring = process.communicate()[0]
    outputlist = outputstring.splitlines()
    blah = outputlist[53]

You need to define cmd -- right now it's trying to execute something like " -j 90 -b 288". I presume you want something like cmd = "program_name -j 90 -b 288".

Don't know if that answers your question at all, but I hope it gives food for thought.

Brian M. Hunt
@Brian they should be integers .The spaces line up the 2nd group of numbers.
Tyler
Why would you prefer lists to tuples in this case? There is no need to use lists
truppo
One reason I prefer lists is to avoid requiring a "," to make "(0)" a tuple (i.e. "(0,)" versus "[0]" always being a list). IMHO the best use of a tuple is where the sequence must be immutable (e.g. a dictionary key). Some people prefer tuples - it's a matter of preference, I suppose. Why would you prefer tuples in this case?
Brian M. Hunt
You're using floating point numbers as keys to a dictionary? I guess this works because you are not really accessing by key but only iterating over items.
hughdbrown
@hughdbrown: I don't think it's a good idea to use floats as keys in this case because of the loss of precision, but they're immutable so ought to be fine otherwise. The args to %f can remedy some concerns about precision, but a string would be better.
Brian M. Hunt