0

I'm working on a bit of a project in Python. I have a list of files that I want to get the md5checksums for. Then it stores the checksums in another list. Then it checks again and checks to see if they're different. I have the function for getting the checksums working but now I can't figure out how I'd add them to a list. Here is what I'm trying


import sys, hashlib
files = ['/home/file1', '/home/file2', '/home/file3', '/etc/passwd']
md5s = []
def getmd5(file, ex="", inc=""):
    m = hashlib.md5()
    try:
        fd = open(file,"rb")
    except IOError:
        print "Can't retrieve MD5sum for ", file
        pass
    content = fd.readlines()
    fd.close()
    for eachLine in content:
        if ex and eachLine.startswith(ex):
            continue
        m.update(eachLine)
    m.update(inc)
    a = m.hexdigest()
    md5s.append(a)

for i in lists: getmd5(i) print md5s

But when I try this I get 4 lists like so:

['729aebf5b3a841d3ef815e297ae2ce07']
['729aebf5b3a841d3ef815e297ae2ce07', '1c9bc3339234fa7d551bdb8da004c8ad']
['729aebf5b3a841d3ef815e297ae2ce07', '1c9bc3339234fa7d551bdb8da004c8ad', '0c01d98119386db13beb1bfdbae7ba2b']
['729aebf5b3a841d3ef815e297ae2ce07', '1c9bc3339234fa7d551bdb8da004c8ad', '0c01d98119386db13beb1bfdbae7ba2b', 'b51c93a2f965b75de903d159720dd6e6']

What I'd like to do is store each hash checksum in the md5s list and then read to see if they're different.

AustinM
  • 773
  • 6
  • 18
  • 27

2 Answers2

2
def getmd5(filename, ....):

  return m.hexdigest()


md5s = dict()

for fname in filenames:
   md5s[fname] = getmd5(fname)

print md5s
1

You don't have 4 lists. You're printing the contents of the md5s each time after you call getmd5 which adds one md5 hash to the list. You just happen to be doing this 4 times, because you have 4 items in your files list.

This means you have one list and it contains all the digests at the end of last for loop. You are printing inside the for loop constructing the list and hence you are not able to understand it. Remove the indent before the print to see the result in the format that you want.

Community
  • 1
  • 1
Alan
  • 45,915
  • 17
  • 113
  • 134
  • Yes but I want to make it so users can append files to the files list. So it must be able to have more than four if the user appends files. – AustinM Feb 27 '11 at 18:20
  • I was just pointing out that you weren't actually creating 4 lists, you have one list, that you print 4 times. – Alan Feb 27 '11 at 18:53