下面這段代碼結(jié)果會(huì)生成以|
分割的文件名洁墙、Hash值儿子、短Hash值的文本文件
airGrooveUmamusume_v10.safetensors | 53378f37d3a7145ece0e6b12cbccf2f51cc85f65894c7f414754369a7d50acb0 | 53378f37d3
0329-2.xlsx | 45cf02b3e0edf3c7e3853ae25c8e860957d2b9329e4ca12473726d98acc88c38 | 45cf02b3e0
import hashlib
import os
# 改為模型所在的單獨(dú)文件夾 我這里是有個(gè)桌面上的models文件夾
fileDocument = '/Users/boxjing/Desktop/models'
# 跑完后會(huì)有個(gè)file_hash.txt 文件洪橘,里面是所有文件的hash值
fileName = 'file_hash.txt'
hash_file = open(fileName, 'w', encoding='utf-8')
for root, dirs, files in os.walk(fileDocument):
if root != fileDocument:
# 子目錄不處理
break
for f in files:
path = os.path.join(root, f)
with open(path, 'rb') as fp:
data = fp.read()
sha256 = hashlib.sha256(data).hexdigest()
shortHash = sha256[0:10]
hashStr = f'{f} | {sha256} | {shortHash}'
print(hashStr)
hash_file.write(hashStr)
hash_file.write('\n')
hash_file.flush()
hash_file.close()