tags:

views:

270

answers:

3

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.

A: 

You should probably use the subprocess module as a replacement for os.system.

hyperboreean
Could you evaluate please?
Filip Ekberg
@Filip Ekberg: what Douglas Leeder said :-)
hyperboreean
+9  A: 

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()
Douglas Leeder
+1 to hashlib, the best way to do it, not depending of an external executable.
nosklo
A: 
import subprocess

p = subprocess.Popen("md5sum gmail.csv", shell=True, stdout=subprocess.PIPE)
resultMD5, filename = p.communicate()[0].split()
print resultMD5
Van Gale
no need to go through the shell to run md5sum. Don't use shell=True
nosklo