1

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.

Jaden-Dz99
  • 119
  • 8

2 Answers2

2

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
wxker
  • 1,841
  • 1
  • 6
  • 16
0

Fixed above error as stated using this line of code:

filez = z[0], b[0]

Jaden-Dz99
  • 119
  • 8
  • 1
    This works because you're using `askopenfilenames` instead of `askopenfilename` (notice the `s` at the end). If you want to have two dialogs, you should use the singular one, otherwise you can use the plural one with a single call and your files will be `[0]` and `[1]` in the return value – ChatterOne Mar 06 '20 at 07:45