views:

215

answers:

1

This is for a script I'm working on. It's supposed to run an .exe file for the loop below. (By the way not sure if it's visible but for el in ('90','52.6223',...) is outside the loop and makes a nested loop with the rest) I'm not sure if the ordering is correct or what not. Also when the .exe file is ran, it spits some stuff out and I need a certain line printed to the screen (hence where you see AspecificLinfe= ... ). Any helpful answers would be great!

for el in ('90.','52.62263.','26.5651.','10.8123.'):

    if el == '90.':
     z = ('0.')
    elif el == '52.62263.':
     z = ('0.', '72.', '144.', '216.', '288.')
    elif el == '26.5651':
     z = ('324.', '36.', '108.', '180.', '252.')
    else el == '10.8123':
     z = ('288.', '0.', '72.', '144.', '216.')

     for az in z:

      comstring = os.path.join('Path where .exe file is')
      comstring = os.path.normpath(comstring) 
      comstring = '"' + comstring + '"'

      comstringfull = comstring + ' -el ' + str(el) + ' -n ' + str(z)

      print 'The python program is running this command with the shell:'
      print comstring + '\n'

      process = Popen(comstring,shell=True,stderr=STDOUT,stdout=PIPE)
      outputstring = myprocesscommunicate()

      print 'The command shell returned the following back to python:'
      print outputstring[0]

       AspecificLine=linecache.getline(' ??filename??   ',   # ??
       sys.stderr.write('az', 'el', 'AREA' )   # ??
+1  A: 

Using shell=True is wrong because that needlessy invokes the shell.

Instead, do this:

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

    for az in z:

        exepath = os.path.join('Path where .exe file is')
        exepath = os.path.normpath(comstring) 
        cmd = [exepath, '-el', str(el), '-n', str(z)]

        print 'The python program is running this command:'
        print cmd

        process = Popen(cmd, stderr=STDOUT, stdout=PIPE)
        outputstring = process.communicate()[0]

        print 'The command returned the following back to python:'
        print outputstring
        outputlist = outputstring.splitlines()
        AspecificLine = outputlist[22]   # get some specific line. 23?
        print AspecificLine
nosklo
Maybe you could have added that if you pass a list-like object to subprocess.Popen it handles the escaping itself, which does not happen if you pass a string. That would have helped understanding why you have used a list in your code ;)
DasIch
@DasIch: I wouldn't say that, because that is wrong. When you run any program, the arguments are *always* passed as a LIST, never as string. It's *your shell* that parses the string, and then splits it into a list before calling the command. The default separator is usually a space, but you can configure that. Shells are written this way for convenience on the command line. The shell has to work hard to split your string into a list, expand globs like "*.txt", etc. Since you're avoiding the shell, you just don't have to escape anything: arguments are passed directly. Popen has to do nothing.
nosklo