K
K
katrushenkov2019-07-07 13:19:25
Python
katrushenkov, 2019-07-07 13:19:25

How to check if a file exists in a directory by sha1?

Hello, tell me how best to implement the script.
It is required that the script writes the sha1 hashes of the files in the directory to txt. At the same time, it checked whether a similar hash was contained (in this case, deleted the file).
Similar methods of checking whether files were present in a directory are welcome to avoid duplicates. Thank you.
Script to write hash of one file to txt:

BUF_SIZE = 65536  # lets read stuff in 64kb chunks!

sha1 = hashlib.sha1()

with open(filename, 'rb') as f:
    while True:
        data = f.read(BUF_SIZE)
        if not data:
            break
        sha1.update(data)

my_file = open("output.txt", "a")
my_file.write('\n' + sha1.hexdigest())
my_file.close()

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question