1

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 :/

6
  • means line 54 is the same thing as blah ... equals sign means = means line 54 is the same thing as etc....... Commented Jun 30, 2009 at 18:37
  • @Tyler, that's rude. What does it MEAN? Why are the words there? What are you trying to do? There are no line numbers in the code sample. What are you talking about? Please clarify your question. Commented Jun 30, 2009 at 18:39
  • 1
    what line is it giving for he InextError. IndexError is thrown when you use an index to a list which doesn't exist. I have a guess that it happens on outputlist. It probably isn't 53 elements long. Try doing 'print len(outputlist)' before the assignment to blah and you'll get how long that list is. Commented Jun 30, 2009 at 18:52
  • 1
    right, so you're list is not 54 element long. outputlist[53] will get the 54th element of the list outputlist. If you want to know how long it really is use what I said above. If you want to findout what all the elements are in that list just use 'print str(outputlist)' Commented Jun 30, 2009 at 19:00
  • 2
    ... madness lies here. May I suggest a book: bit.ly/oLfXD It's how I learned Python. Commented Jun 30, 2009 at 19:08

4 Answers 4

6

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.

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

5 Comments

@Brian they should be integers . The spaces line up the 2nd group of numbers.
Why would you prefer lists to tuples in this case? There is no need to use lists
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?
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: 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.
2

Are you sure this is right

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

Where's your executable?

8 Comments

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 72 etc etc etc Executable is in program files a folder a folder then the .exe file
but how are you telling Popen where to find your .exe? It's not in cmd.
exepath = os.path.join('EXE file location) exepath = '"' + os.path.normpath(exepath) + '"' print exepath I do that before the for statement mentioned here. How do I get it to print that?
@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?
@Tyler: Don't answer in the comments. Fix your question. It's your question. Fix it, please.
|
2

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.

2 Comments

Though if that was the whole problem, that would just give an UnboundLocal exception.
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
1

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.

6 Comments

I move prcoess one line ahead of outputsring = process.communicate()[0] and it tells me cmd isn't defined
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.
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.
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: Order Matters. cmd= then process= . In that order.
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.