one of my function in the program checks the md5sum of hashfile
def check():
print "checking integrity status.."
md5 = subprocess.Popen(["md5sum", "-c", hashfile],shell=False, stdout=subprocess.PIPE)
#fopen = open(basefile, "r")
for f in md5.stdout.readlines():
fc = f.rstrip("\n")
sys.stdout.write("\rChecking..." + fc)
sys.stdout.flush()
now what happens is that the whole command is first executed and then for loop reads from md5 using md5.stdout.readlines, as such it is not dynamic i.e. i dont get the output as the command is executed….is there a way i can get the output while the command is in execution…
fixed using glglgl’s answer:
def check():
print "checking integrity status.."
md5 = subprocess.Popen(["md5sum", "-c", hashfile],shell=False, stdout=subprocess.PIPE)
#fopen = open(basefile, "r")
fc = "NULL"
i = 0
for f in iter(md5.stdout.readline, ''):
k = fc
fc = f.rstrip("\n")
if "FAILED" in fc:
print fc
i = i + 1
sys.stdout.write("\rChecking... "+ str(i)+ " " + fc + (' ' * (len(k) - len(fc))) )
sys.stdout.flush()
Of course. There are several ways.
First, you could work with
md5.stdout.read(), but there you would have to do line separation by yourself.Then, you could operate with the file object
md5.stdoutas iterator. But there seems to be an issue with buffering, i. e. you don’t get the results immediately.And then there is a possibility to call
md5.stdout.readline()repeatedly until it returns''.The third way is to be preferred in this case; I would suggest it to do like this:
…
I have also changed the output text, as there is only an output if che check is done already.
If that is not what you want, but rather really haveing every output captured separately, you should switch to point 1. But that makes it more complicated. I will think about a solution on indication that it is wanted.
There, one must consider the following points:
read()blocks, so one should read byte for byte (quite ugly).