-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathget_checksum_filesize.py
42 lines (34 loc) · 1.02 KB
/
get_checksum_filesize.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import sys
import os
import hashlib
"""
Utility script for getting the file size in bytes and the SHA-256 checksum for all files in a directory
"""
def main():
try:
rootdir = sys.argv[1]
except Exception:
print("Please provide a target directory path in the first argument!")
exit(-1)
print(f"{'Filename':<70}{'Size (bytes)':<30}{'SHA-256 checksum':<30}")
print("-"*165)
print()
for root, dirs, filenames in os.walk(rootdir):
for filename in filenames:
filepath = root + os.sep + filename
stat = os.stat(filepath)
shasum = sha256sum(filepath)
print(f"{filename:<70}{stat.st_size:<30}{shasum:<30}")
print("-"*165)
break
def sha256sum(filename):
h = hashlib.sha256()
with open(filename, 'rb') as file:
while True:
chunk = file.read(h.block_size)
if not chunk:
break
h.update(chunk)
return h.hexdigest()
if __name__ == "__main__":
main()