First - you're comparing file names - calling update
in Hashlib classes does not open files alone - it expects bytes objects (and that's the reason for the error message, you're passing text) - but even if you put the b'
prefix in these name strings, or use .encode()
in them, you will continue to have the file name only hash. (Another error: you used twice the same variable name - if you were opening the files, would be comparing the "b.txt" file with itself)
To see the hash of the contents of the files do:
import hashlib
file_0 = 'a.txt'
file_1 = 'b.txt'
hash_0 = hashlib.new('ripemd160')
hash_0.update(open(file_0, 'wb').read())
hash_1 = hashlib.new('ripemd160')
hash_0.update(open(file_1, 'wb').read())
if hash_0.digest() != hash_1.digest():
print(f'O arquivo: {file_0} é diferente do arquivo: {file_1} ')
(You were also using assert
wrongly. Avoid using assert
in same code - and reserve this command for testing.
Although it looks like a shortening of if
followed by raise
in some places, it is a test that is disabled depending on the parameters with which the Python runtime runs - so there is a lot developer setting up assert
in production code that can be more difficult, with a test that is not done, because of a seemingly innocuous configuration changed elsewhere)