_________ / Zatta /file_renamer.py
Default38693's picture
Upload 8 files
ffc9a51
raw
history blame
860 Bytes
import hashlib
from pathlib import Path
#指定ディレクトリのpngファイル名をmd5に変更するやつ。全角とかスペースの考慮がだるいときに
def md5_hash(file_path):
with open(file_path, 'rb') as f:
file_data = f.read()
md5_hash = hashlib.md5(file_data).hexdigest()
return md5_hash
def rename_files_in_directory(directory_path):
dir_path = Path(directory_path)
for file_path in dir_path.glob('*.png'):
new_file_name = md5_hash(file_path) + ".png"
new_file_path = file_path.parent / new_file_name
file_path.rename(new_file_path)
print(f"Renamed {file_path} to {new_file_path}")
if __name__ == "__main__":
directory_path = r"E:\Dataset\XXXXXXX" # ここにディレクトリへのパスを入力してください
rename_files_in_directory(directory_path)