2009-06-01 101 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