import hashlibimport osdef get_sha1f with openE练习11txt rb as file content = fileread sha1 = hashlibsha1contenthexdigest with openE练习11txt a as file filewriten filewri
在调用get_sha1函数时,传递给函数的参数应该是文件路径字符串,而不是文件对象。另外,get_sha1函数中的文件路径应该使用原始字符串(raw string)的形式,即在字符串前面加上r,以避免反斜杠被转义。以下是修改后的代码:
import hashlib
def get_sha1(file_path):
with open(file_path, 'rb') as file:
content = file.read()
sha1 = hashlib.sha1(content).hexdigest()
with open(file_path, 'a') as file:
file.write('\n')
file.write(sha1)
get_sha1(r'E:\练习1\1.txt')
请确保1.txt文件存在于E:\练习1\目录中,并且具有读写权限。
原文地址: https://www.cveoy.top/t/topic/ieRf 著作权归作者所有。请勿转载和采集!