First - you are comparing file names - the call to update
in Hashlib classes does not open files alone - it expects objects of type "bytes" (and that is the reason for the error message, you are passing text) - but even if you put the prefix b'
in these name strings, or use .encode()
in the same, will continue to hash only the name of the files. (Another error: you used twice the same variable name - if you were opening the files, would be comparing the file "b. txt" 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, 'rb').read())
hash_1 = hashlib.new('ripemd160')
hash_1.update(open(file_1, 'rb').read())
if hash_0.digest() != hash_1.digest():
print(f'O arquivo: {file_0} é diferente do arquivo: {file_1} ')
(you were also using the assert
wrong way. Avoid using the assert
in the same code - and reserve this command for testing.
Although it seems a shortening of a if
followed by a raise
in some places, it is a test that is disabled depending on the parameters with which Python Runtime runs - so there is a lot of developer there assert
in a production code that can get in trouble sooner or later, with a test that is not done, because of an apparently innocuous configuration changed elsewhere)
Do you need to compare the contents of the files or their names only? The way you are doing you will only compare the names. The method
update
accepted only bytes-like as a parameter, then you will need to encode your string with the methodencode
.– Woss
I need to compare the contents of the file. I will continue searching and find how to compare the content. Thank you.
– Igor Gabriel