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()
        print m.hexdigest()

Recommended Answers

All 4 Replies

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!

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()
            print '\n', m.hexdigest()+ '\n'

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:
    print '\n', m.hexdigest()+ '\n'

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.