0

I am trying to get the md5 hash of a file. It seems the md5 I am getting from the script is different than the actual md5. If anyone could point me in the direction needed it would be great.

  for dir, dirs, file in os.walk(path)
    if file == (filename):
        m = hashlib.md5()
        m.update(file)  
        m.hexdigest()
        print m.hexdigest()
2
Contributors
4
Replies
8
Views
5 Years
Discussion Span
Last Post by crag0
1

The third item in os.walk is a list of filenames. Otherwise, the md5 of a file refers to the content of the file, not to the filename. Run your code before posting issues!

0

Maybe I should explain more. I am looking to only md5 the files called sample. I don't doubt things are working how they should but more knowing the lack of really understanding things on my side.

for dirs, dir, files in os.walk(new_dir):
    for file in files:
        if file == ('sample'):
            m = hashlib.md5()
            m.update(file)
            m.hexdigest()
            print '\n', m.hexdigest()+ '\n'
0

It should be something like

if file == 'sample':
    m = hashlib.md5()
    with open(file, 'rb') as ifh:
        while True:
            s = ifh.read(1024)
            if s:
                m.update(s)
            else:
                break
    print '\n', m.hexdigest()+ '\n'

Edited by Gribouillis

0

Thanks Gribouillis, I realize the mistake of not opening the file. Things are working as I was hoping.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.