Geoff Alexander
Geoff Alexander

Reputation: 502

Problem with capaturing Python output from a Python script containing subprocess.call()

Here's my problem. When attempting to capture Python output, subprocess.call() output incorrectly comes before print() output. I'm running Python from a command window using Python 3.7.2 on Windows 10.

Here's a small example illustrating the problem:

import subprocess

print("test")
subprocess.call("where python",shell=True)

Note that the print() output should come before the subprocess.call() output.

Here's what I get when I run without capturing output:

c:\Users\GeoffAlexander\Documents\Python>python test.py
test
C:\Users\GeoffAlexander\AppData\Local\Programs\Python\Python37-32\python.exe>
c:\Users\GeoffAlexander\Documents\Python>

The print() output correctly comes before the subprocess.call() output as expected.

However when I redirect the output to a file, here's what I get:

c:\Users\GeoffAlexander\Documents\Python>python test.py > test.out

c:\Users\GeoffAlexander\Documents\Python>cat test.out
C:\Users\GeoffAlexander\AppData\Local\Programs\Python\Python37-32\python.exe
test

c:\Users\GeoffAlexander\Documents\Python>

Note that the subprocess.call() output incorrectly comes before the print() output.

Why does this happen? How can I capture the Python output in the correct order?

Upvotes: 1

Views: 84

Answers (2)

J. Smith
J. Smith

Reputation: 108

How can I capture the Python output in the correct order?

Hello,

Well try flushing your standard output after your print call, like:

import subprocess
import sys

print("test")
sys.stdout.flush()
subprocess.call("echo python",shell=True)

The output will be correct (tested on Linux system):

$ python test.py
test
python
$ python test.py > filename
$ cat filename
test
python

Upvotes: 1

balderman
balderman

Reputation: 23825

You need to communicate with the subprocess via a pipe

See https://www.endpoint.com/blog/2015/01/28/getting-realtime-output-using-python for an example

Upvotes: 0

Related Questions