Tyler
Tyler

Reputation: 4017

Python and subprocess

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' )           # ??

Upvotes: 0

Views: 2276

Answers (1)

nosklo
nosklo

Reputation: 223152

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

Upvotes: 1

Related Questions