Search code examples
pythonchecksum

How to create a checksum of a file in python


I am trying to create a checksum of a file and save the checksum as a file same. So.I monitor the the file and if the checksum changes then a do something.

Here is the checksum

For test.txt

contents: a
checksum: dd18bf3a8e0a2a3e53e2661c7fb53534

I edit the file:

contents: aa
checksum: dd18bf3a8e0a2a3e53e2661c7fb53534

here is my code:

python -c 'import hashlib;print hashlib.md5("test.txt").hexdigest()'

Why are the checksums the same?


Solution

  • You may try to check hashlib.md5()

    import hashlib
    [(fname, hashlib.md5(open(fname, 'rb').read()).digest()) for fname in fnamelst]