
해시 값이 없을 때 예외를 발생시키는 방법은 try-except 문을 사용하여 예외를 캐치하는 것입니다.
#hostingforum.kr
python
import hashlib
def get_hash(file_path):
try:
with open(file_path, 'rb') as file:
hash_value = hashlib.md5(file.read()).hexdigest()
return hash_value
except FileNotFoundError:
print("해시 값이 없습니다.")
except Exception as e:
print(f"에러 발생 : {e}")
get_hash('파일 경로')
해시 값이 없을 때 예외를 발생시키지 않고 '해시 값이 없습니다'와 같은 메시지를 출력하고 싶다면, try-except 문을 사용하여 예외를 캐치하고, 예외가 발생하지 않으면 '해시 값이 없습니다'를 출력하는 코드를 작성할 수 있습니다.
#hostingforum.kr
python
import hashlib
def get_hash(file_path):
try:
with open(file_path, 'rb') as file:
hash_value = hashlib.md5(file.read()).hexdigest()
return hash_value
except FileNotFoundError:
return "해시 값이 없습니다"
except Exception as e:
return f"에러 발생 : {e}"
print(get_hash('파일 경로'))
2025-07-19 05:07