Search code examples
pythonmd5tk-toolkithashlib

Select and compare two files MD5 using Python


I would like to manipulate the code from an answer found in the following link: Compare md5 hashes of two files in python

My expected outcome would be to search for the two files I want to compare, and then execute the rest of the script allowing for an answer of whether it is "True" that the MD5 files matches, otherwise "False".

I have tried the following code:

import hashlib
from tkinter import *
from tkinter import filedialog

digests = []

z = filedialog.askopenfilenames(initialdir="/", title="Browse Files", filetypes=(("excel files", "*.xlsx"),
                                                                                             ("all files", "*.*")))

b = filedialog.askopenfilenames(initialdir="/", title="Browse Files", filetypes=(("excel files", "*.xlsx"),
                                                                                             ("all files", "*.*")))
filez = z, b

for filename in filez:
    hasher = hashlib.md5()
    with open(filename, 'rb') as f:
        buf = f.read()
        hasher.update(buf)
        a = hasher.hexdigest()
        digests.append(a)
        print(a)

print(digests[0] == digests[1])

Unfortunately I receive the following error: "TypeError: expected str, bytes or os.PathLike object, not tuple"

Thanks in advance.


Solution

  • Fixed above error as stated using this line of code:

    filez = z[0], b[0]