0

Currently, I am using the following format to write the run results to a log file.

p = subprocess.run(["mpiexec -n 2 ./executor >log"],shell=True)

Could anyone tell me how to avoid using the "shell=True" while I can write a log file?

Thank you.

NoVel
  • 21
  • 7
  • Something like https://stackoverflow.com/questions/25750468/displaying-subprocess-output-to-stdout-and-redirecting-it ? – ewokx Jul 07 '20 at 01:32
  • 1
    @ewong, that question is distinct because it's trying to redirect to a file *while also getting a live stream*; the OP here doesn't require that live stream (and neither does the duplicate I closed the question with). – Charles Duffy Jul 07 '20 at 01:55

1 Answers1

0

Just split the arguments yourself, open the file yourself, and pass the open file to run to make it send the output there:

with open('log', 'wb') as outfile:
    p = subprocess.run(['mpiexec', '-n', '2', './executor'], stdout=outfile)
ShadowRanger
  • 143,180
  • 12
  • 188
  • 271