Python – Getting Python System Calls as string results


I'd like to use os.system("md5sum myFile") and have the result returned from os.system instead of just runned in a subshell where it's echoed.

In short I'd like to do this:

resultMD5 = os.system("md5sum myFile")

And only have the md5sum in resultMD5 and not echoed.

Best Solution

subprocess is better than using os.system or os.popen

import subprocess
resultMD5 = subprocess.Popen(["md5sum","myFile"],stdout=subprocess.PIPE).communicate()[0]

Or just calculate the md5sum yourself with the hashlib module.

import hashlib
resultMD5 = hashlib.md5(open("myFile").read()).hexdigest()