qed
qed

Reputation: 23134

Print output of external command in realtime and have it in a string at the same time in python

For example:

#!/usr/bin/env python3

# cmd.py

import time
for i in range(10):
    print("Count %d" % i)
    time.sleep(1)

#!/usr/bin/env python3

import subprocess

# useCmd.py

p = subprocess.Popen(['./cmd.py'], stdout=subprocess.PIPE)
out, err = p.communicate()
out = out.decode()
print(out)

In useCmd.py I can print out the output of cmd.py, but only after it's finished outputting. How can I print out it in realtime and still have it stored in a string? (sort of like tee in bash.)

Upvotes: 0

Views: 72

Answers (1)

Dariosky
Dariosky

Reputation: 531

If you don't have to deal with stdin, you could avoid using communicate that is blocking, and read directly from the process stdout until your stdout ends:

p = subprocess.Popen(['python', 'cmd.py'], stdout=subprocess.PIPE)
# out, err = p.communicate()
while True:
    line = p.stdout.readline()
    if line != '':
        print line,
    else:
        break

related

Upvotes: 1

Related Questions