Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Python System Calls as string results

Tags:

python

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.

like image 259
Filip Ekberg Avatar asked Apr 24 '09 09:04

Filip Ekberg


1 Answers

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()
like image 175
Douglas Leeder Avatar answered Sep 18 '22 22:09

Douglas Leeder