On Windows systems, md5sum.exe is a nice little program to generate MD5 sums. However, whoever created this application forgot to add the ability to recognize paths in the file given to md5sum.exe. For example, say you want to md5 a file called test.txt.
C:\test>dir
Directory of C:\test
06/13/2007 03:52 PM <DIR> .
06/13/2007 03:52 PM <DIR> ..
06/13/2007 03:52 PM 0 test.txt
1 File(s) 0 bytes
C:\test>md5sum test.txt
d41d8cd98f00b204e9800998ecf8427e *test.txt
Groovy, cool...
However, if you try the same thing from some other directory (where text.txt does not live) you get the following:
C:\>md5sum c:/test/test.txt
md5sum: test.txt: No such file or directory
Sucky, eh?
You could always find a different program to do md5sum. Or, if you don't want to do this, you can spawn a pipe to "cmd", navigate to the correct directory, and then run md5sum.
Here's the code in python to do just that.
file_name = "afile.txt"
file_path = "c:\\afolder"
md5_cmd = "md5sum \"" + file_path "\\" + file_name + "\"\n"
fromchild, tochild = popen2.popen4("cmd")
tochild.write("c:\nchdir " + my_path + "\n" + md5_cmd + "exit\n")
tochild.close()
out = fromchild.read()
output = string.split(out)
md5sum_local = ""
for item in output:
matchmd5local = re.match("([0-9a-fA-F]{32})",item)
if matchmd5local:
md5sum_local = matchmd5local.groups()
md5sum_local = md5sum_local[0]
if md5sum_local:
print_status("MD5: Local checksum = " + md5sum_local + "\n")
else:
print_status("ERROR: could not obtain local md5sum for " + my_file + "\n")