Jaden-Dz99
Jaden-Dz99

Reputation: 139

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.

Upvotes: 1

Views: 146

Answers (2)

wxker
wxker

Reputation: 1896

The filedialog.askopenfilenames returns a tuple. This means means that z and b, and in turn the filename iterator of the for loop, are tuples. You are getting the error because you are passing filename, which is a tuple, into the open function.

A way to fix this could be to simply concatenate the tuples.

filez = z + b

Upvotes: 2

Jaden-Dz99
Jaden-Dz99

Reputation: 139

Fixed above error as stated using this line of code:

filez = z[0], b[0]

Upvotes: 0

Related Questions