<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: pipe code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 08 Aug 2008 19:28:46 GMT</pubDate>
    <description>DZone Snippets: pipe code</description>
    <item>
      <title>Making md5sum.exe Work with Paths in Python</title>
      <link>http://snippets.dzone.com/posts/show/4139</link>
      <description>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.&lt;br /&gt;&lt;br /&gt;C:\test&gt;dir&lt;br /&gt; Directory of C:\test&lt;br /&gt;&lt;br /&gt;06/13/2007  03:52 PM    &lt;DIR&gt;          .&lt;br /&gt;06/13/2007  03:52 PM    &lt;DIR&gt;          ..&lt;br /&gt;06/13/2007  03:52 PM                 0 test.txt&lt;br /&gt;               1 File(s)              0 bytes&lt;br /&gt;&lt;br /&gt;C:\test&gt;md5sum test.txt&lt;br /&gt;d41d8cd98f00b204e9800998ecf8427e *test.txt&lt;br /&gt;&lt;br /&gt;Groovy, cool...&lt;br /&gt;&lt;br /&gt;However, if you try the same thing from some other directory (where text.txt does not live) you get the following:&lt;br /&gt;&lt;br /&gt;C:\&gt;md5sum c:/test/test.txt&lt;br /&gt;md5sum: test.txt: No such file or directory&lt;br /&gt;&lt;br /&gt;Sucky, eh?&lt;br /&gt;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.&lt;br /&gt;&lt;br /&gt;Here's the code in python to do just that.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# define your file name and path&lt;br /&gt;file_name = "afile.txt"&lt;br /&gt;file_path = "c:\\afolder"&lt;br /&gt;&lt;br /&gt;# setup the md5sum command (assuming md5sum.exe location is in PATH)&lt;br /&gt;md5_cmd = "md5sum \"" + file_path "\\" + file_name + "\"\n"&lt;br /&gt;&lt;br /&gt;# open the command shell&lt;br /&gt;fromchild, tochild = popen2.popen4("cmd")&lt;br /&gt;&lt;br /&gt;#push the directory change and md5sum commands to the shell &lt;br /&gt;tochild.write("c:\nchdir " + my_path + "\n" + md5_cmd + "exit\n")&lt;br /&gt;tochild.close()&lt;br /&gt;&lt;br /&gt;#get the output from the shell session&lt;br /&gt;out = fromchild.read()&lt;br /&gt;&lt;br /&gt;#split the output so that we may extract the md5sum&lt;br /&gt;output = string.split(out)&lt;br /&gt;&lt;br /&gt;#grab the md5sum  &lt;br /&gt;md5sum_local = ""&lt;br /&gt;for item in output:&lt;br /&gt;    matchmd5local = re.match("([0-9a-fA-F]{32})",item)&lt;br /&gt;    if matchmd5local:&lt;br /&gt;        md5sum_local = matchmd5local.groups()&lt;br /&gt;        md5sum_local = md5sum_local[0]&lt;br /&gt;&lt;br /&gt;if md5sum_local:&lt;br /&gt;    print_status("MD5: Local checksum = " + md5sum_local + "\n")&lt;br /&gt;else:&lt;br /&gt;    print_status("ERROR: could not obtain local md5sum for " + my_file + "\n")&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 13 Jun 2007 23:10:02 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4139</guid>
      <author>minitotoro (Natalie)</author>
    </item>
  </channel>
</rss>
