2009-06-01 68 views
0

这是为我正在处理的脚本。它应该为下面的循环运行一个.exe文件。 (顺便说一句,不确定它是否可见,但在('90','52.6223',...)中的el在循环之外,并与其他循环一起构成嵌套循环)我不确定排序是否正确或什么不是。另外,当.exe文件运行时,它会吐出一些东西,我需要在屏幕上打印某一行(因此,您看到AspecificLinfe = ...)。任何有用的答案都会很棒!Python和子进程

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

使用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 
+0

也许你可能会补充说,如果你传递一个列表类似的对象subprocess.Popen它处理逃逸本身,如果你传递一个字符串,它不会发生。这将有助于理解你为什么在你的代码中使用了一个列表;) – DasIch 2009-06-01 22:17:53