I would like to count the number of lines written to stdout
by a process (here unrar.exe) created with Popen
.
import time
from subprocess import Popen, PIPE, STDOUT
p = Popen('unrar.exe x -y myfile.rar', stdout=PIPE)
while (p is not finished): # pseudo code here and next lines...
time.sleep(0.100)
print 'Number of lines written to STDOUT by unrar' + len(PIPE.split('\n'))
How to do this properly ?
Remark : I already looked at p.communicate()
(https://python.readthedocs.org/en/v2.7.2/library/subprocess.html#subprocess.Popen.communicate) but this has the effect of blocking the execution of the Python until p
has terminated, which is not what I want : I want to be able to print the number of lines written by p
when it's running.