34 lines
1006 B
Python
34 lines
1006 B
Python
import hashlib
|
|
|
|
def calculate_sha1(filepath):
|
|
"""
|
|
Calculates the SHA1 hash of a given file.
|
|
|
|
Args:
|
|
filepath (str): The path to the file.
|
|
|
|
Returns:
|
|
str: The hexadecimal representation of the SHA1 hash, or None if the file is not found.
|
|
"""
|
|
sha1_hash = hashlib.sha1()
|
|
try:
|
|
with open(filepath, "rb") as f:
|
|
# Read the file in chunks to handle large files efficiently
|
|
for chunk in iter(lambda: f.read(4096), b""):
|
|
sha1_hash.update(chunk)
|
|
return sha1_hash.hexdigest()
|
|
except FileNotFoundError:
|
|
print(f"Error: File not found at {filepath}")
|
|
return None
|
|
except Exception as e:
|
|
print(f"An error occurred: {e}")
|
|
return None
|
|
|
|
|
|
if __name__ == "__main__":
|
|
# Example usage:
|
|
file_path = "xls.xls" # Replace with the actual path to your file
|
|
sha1_result = calculate_sha1(file_path)
|
|
|
|
if sha1_result:
|
|
print(f"The SHA1 hash of '{file_path}' is: {sha1_result}") |