Python subprocess reads stdout as it executes

one of my functions in the program checks the md5sum file hash

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 it happens that the whole command is executed first, and then to read the loop from md5 using md5.stdout.readlines, therefore it is not dynamic, i.e. I don't get output as the command executes .... there is a way I can get the output while the command is executing ...

fixed using glglgl 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() 
+4
source share
3 answers

Sure. There are several ways.

  • First, you can work with md5.stdout.read() , but you will have to do the line splitting yourself.

  • Then you can work with the md5.stdout file object as an iterator. But there seems to be a buffering problem, i.e. E. You will not receive the results immediately.

  • And then it is possible to call md5.stdout.readline() several times until it returns. ''

A third method should be preferred in this case; I suggest doing this as follows:

...

 for f in iter(md5.stdout.readline, ''): fc = f.rstrip("\n") sys.stdout.write("\rChecked " + fc) sys.stdout.flush() 

I also changed the output text as there is only output if che check is already done.

If this is not what you need, but, indeed, each individual conclusion that will be drawn separately, you must go to step 1. But this complicates the situation. I will think about the decision that it is needed.

The following points should be considered here:

  • read() , so you need to read byte for byte (rather ugly).
  • The question arises, what should be output, and when should be intermittent output.
+5
source

The original poster is true that hashlib is not available in Python 2.4, but the md5 library is available. Example workaround:

 try: # Python 2.5 and up. import hashlib md5Hash = hashlib.md5 except ImportError: # Python 2.4 and below. import md5 md5Hash = md5.new somedata = 'foobar' hashstring = md5Hash (somedata).hexdigest () 
+1
source

What is the file size?

Popen creates a new child process to run the command. It may end before starting the for loop.

You can check if the "subprocess" completed the search for the returncode attribute (in your code: md5.returncode)

0
source

All Articles