0

This question is related to my previous question.

Error of running an executable file from Python subprosess

I am trying to run an executable file (a linear programming solver CLP.exe) from Python 3.5.

 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)

When I run the python file in Eclipse PyDev, I can see the results in Eclipse console and the solution results are also saved in "solution.txt".

But, no log results are saved at the file of "log.txt".

In the Eclipse console, I got:

  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

When I run the command in MS windows shell from command line:

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

I can get log results in log.txt.

Why the log.txt file was not created and no log results were saved to it if I run the command from Python subprocess ?

Community
  • 1
  • 1
usa
  • 43
  • 3

1 Answers1

0

You can replace your default stdout descriptor with your log file descriptor. Try this

with open('log.txt', 'w+') s fid:
    subprocess.check_call([arg1,...,arg8],stdout = fid, stderr=subprocess.STDOUT, shell = False)
Da Qi
  • 615
  • 5
  • 10
  • I got error: raise ValueError('stdout argument not allowed, it will be overridden.') ValueError: stdout argument not allowed, it will be overridden. – usa Aug 17 '16 at 22:35
  • I cannot guarantee this, but can you try subprocess.check_call instead of check_output – Da Qi Aug 17 '16 at 22:50