通过从Python子句运行可执行文件将没有日志结果的错误转储到文件

时间:2016-08-17 22:05:38

标签: python python-3.x subprocess pydev

这个问题与我之前的问题有关。

Error of running an executable file from Python subprosess

我正在尝试从Python 3.5运行可执行文件(线性编程解算器CLP.exe)。

 Import subprocess

 exeFile = "C:\\MyPath\\CLP.exe"
 arg1 = "C:\\Temp\\LpModel.mps"
 arg2 = "-max"
 arg3 = "-dualSimplex"
 arg4 = "-printi"
 arg4a = "all"
 arg5 = "-solution"
 arg6 = "solutionFile.txt"
 arg7 = "-log"
 arg8 = "6"
 arg9 = ">"
 arg10 = "log.txt"
 subprocess.check_output([exeFile, arg1, arg2, arg3, arg4a, arg5,arg6, arg7, arg8, arg9, arg10],    stderr=subprocess.STDOUT, shell=False)

当我在Eclipse PyDev中运行python文件时,我可以在Eclipse控制台中看到结果,解决方案结果也保存在“solution.txt”中。

但是,没有日志结果保存在“log.txt”文件中。

在Eclipse控制台中,我得到了:

  b'Coin LP version 1.16, build Dec 25 2015
  command line - C:\\MyPath\\clp.exe C:\\Temp\\LpModel.mps  -max     -dualSimplex  -printi all  -solution C:\\Temp\\solutionFile.txt > log.txt 

  Optimal - objective value 118816.5
  Optimal objective 110 - 40 iterations time 0.022
  logLevel was changed from 1 to 6
  No match for > - ? for list of commands
  No match for log.txt - ? for list of commands

当我从命令行在MS Windows shell中运行命令时:

    C:\\MyPath\\clp.exe C:\\Temp\\LpModel.mps  -max  -dualSimplex  -printi all  -solution C:\\Temp\\solution.txt > log.txt

我可以在log.txt中获取日志结果。

如果我从Python子进程运行命令,为什么没有创建log.txt文件并且没有保存日志结果?

1 个答案:

答案 0 :(得分:0)

您可以使用日志文件描述符替换默认的stdout描述符。试试这个

with open('log.txt', 'w+') s fid:
    subprocess.check_call([arg1,...,arg8],stdout = fid, stderr=subprocess.STDOUT, shell = False)