bobby
bobby

Reputation: 455

How to compare two versions of same file (old and new), and detect if there were some changes?

I am new at both, python and stackoverflow, so please have that on mind. I tried to do this myself and manage to do it, but it works only if i hardcode hash number of previous version like this one in hash1, and then compare with hash number of currently version. I wolud like that program every time save hash number of currently version and then with every run compare it with newer version, and if the file is changed do something.

This is my code

import hashlib

hash1 = '3379b3b9b9c82650831db2aba0cf4e99'

hasher = hashlib.md5()
    with open('word.txt', 'rb') as afile:
    buf = afile.read()
    hasher.update(buf)
    hash2 = hasher.hexdigest()

if hash1 == hash2:
    print('same version')
else
    print('diffrent version')

Upvotes: 2

Views: 1314

Answers (2)

David
David

Reputation: 6561

For relatively simple comparisons, use filecmp. For finer control and feedback, use difflib, which is similar to the *nix utility, diff.

Upvotes: 1

Cannon Moyer
Cannon Moyer

Reputation: 92

Just simply save the hash to a file like file.txt and then when you need to compare a hash just read from your file.txt and compare the two strings. Here is an example of how to read and write to files in python. http://www.pythonforbeginners.com/files/reading-and-writing-files-in-python

Upvotes: 2

Related Questions