Python 和子进程

发布于 2024-07-23 11:31:21 字数 1323 浏览 3 评论 0原文

这是我正在编写的脚本。 它应该为下面的循环运行一个 .exe 文件。 (顺便说一下,不确定它是否可见,但 for el in ('90','52.6223',...) 在循环之外并与其余部分形成嵌套循环)我不确定顺序是否正确或什么不。 另外,当 .exe 文件运行时,它会吐出一些内容,我需要将某一行打印到屏幕上(因此您会看到 AspecicLinfe= ... )。 任何有用的答案都会很棒!

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只是在用心讲痛 2024-07-30 11:31:21

使用 shell=True 是错误的,因为这会不必要地调用 shell。

相反,请执行以下操作:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文