20

In a script , I want to run a .exe with some command line parameters as "-a",and then

redirect the standard output of the program to a file?

How can I implement that?

5 Answers 5

29

You can redirect directly to a file using subprocess.

import subprocess
with open('output.txt', 'w') as output_f:
    p = subprocess.Popen('Text/to/execute with-arg',
                         stdout=output_f,
                         stderr=output_f)
Sign up to request clarification or add additional context in comments.

Comments

11

Easiest is os.system("the.exe -a >thefile.txt"), but there are many other ways, for example with the subprocess module in the standard library.

1 Comment

It's worth remembering the pipe symbol differs between operating systems when trying to use os.system to redirect things...
3

You can do something like this e.g. to read output of ls -l (or any other command)

p = subprocess.Popen(["ls","-l"],stdout=subprocess.PIPE)
print p.stdout.read() # or put it in a file

you can do similar thing for stderr/stdin

but as Alex mentioned if you just want it in a file, just redirect the cmd output to a file

1 Comment

If it's a long running process, be aware that stdout.read() is blocking. You'll want to run the subprocess on a different thread if the script is going to continue doing anything else or respond to the output 'live'.
0

If you just want to run the executable and wait for the results, Anurag's solution is probably the best. I needed to respond to each line of output as it arrived, and found the following worked:

1) Create an object with a write(text) method. Redirect stdout to it (sys.stdout = obj). In your write method, deal with the output as it arrives.

2) Run a method in a seperate thread with something like the following code:

    p = subprocess.Popen('Text/to/execute with-arg', stdout=subprocess.PIPE,
                        stderr=subprocess.PIPE, shell=False)
    while p.poll() is None:
        print p.stdout.readline().strip()

Because you've redirected stdout, PIPE will send the output to your write method line by line. If you're not certain you're going to get line breaks, read(amount) works too, I believe.

3) Remember to redirect stdout back to the default: sys.stdout = __sys.stdout__

Comments

0

Although the title (.exe) sounds like it's a problem on Windows. I had to share that the accepted answer (subprocess.Popen() with stdout/stderr arguments) didn't work for me on Mac OS X (10.8) with python 2.7.

I had to use subprocess.check_output() (python 2.7 and above) to make it work. Example:

import subprocess

cmd = 'ls -l'
out = subprocess.check_output(cmd, shell=True)
with open('my.log', 'w') as f:
    f.writelines(out)
    f.close()

Note that this solution writes all the accumulated output out when the program finishes. If you want to monitor the log file during the run. You may want to try something else. In my own case, I only cared about the end result.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.