import json
import hashlib
def calculate_md5(file_path):
"""ファイルのMD5ハッシュを計算する"""
hash_md5 = hashlib.md5()
with open(file_path, "rb") as f:
for chunk in iter(lambda: f.read(4096), b""):
hash_md5.update(chunk)
return hash_md5.hexdigest()
def add_md5_to_json(json_file_path, md5_value):
"""JSONファイルにMD5ハッシュ値を追加する"""
with open(json_file_path, 'r') as f:
data = json.load(f)
data["x-amz-meta-md5"] = md5_value
with open(json_file_path, 'w') as f:
json.dump(data, f, indent=4)
# ファイルのパス
file_path = 'path/to/your/file'
json_file_path = 'path/to/your/jsonfile.json'
# MD5ハッシュ値を計算
md5_value = calculate_md5(file_path)
# JSONファイルにMD5ハッシュ値を追加
add_md5_to_json(json_file_path, md5_value)
print(f"MD5 hash {md5_value} added to JSON file.")
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme