Skip to content

Instantly share code, notes, and snippets.

@xardit
Created December 24, 2023 15:09
Show Gist options
  • Save xardit/0cc65e0a6c984d1a6e17e5739c09da41 to your computer and use it in GitHub Desktop.
Save xardit/0cc65e0a6c984d1a6e17e5739c09da41 to your computer and use it in GitHub Desktop.
Remove recursively md5 hash string from filenames - Python script
"""
Remove recursively md5 hash string from filenames on files and directories from notion export
and reuse them directly on obsidian vault.
The md5 string is added by Notion on export by default!
@by Ardit
"""
import os
import re
def rename_files_with_md5_hash(directory):
regex = re.compile(r'.*(\s[a-fA-F0-9]{32}).*')
for root, dirs, files in os.walk(directory):
for filename in files:
match = regex.match(filename)
if match:
md5_hash = match.group(1)
new_filename = filename.replace(md5_hash, '')
os.rename(os.path.join(root, filename), os.path.join(root, new_filename))
for d in dirs:
match = regex.match(d)
if match:
md5_hash = match.group(1)
new_dirname = d.replace(md5_hash, '')
os.rename(os.path.join(root, d), os.path.join(root, new_dirname))
# Rename subdirectories
subdirectory = os.path.join(root, d)
if os.path.isdir(subdirectory):
rename_files_with_md5_hash(subdirectory) # Recursive call for subdirectories
# Usage example:
rename_files_with_md5_hash('/some/dir/Obsidian/Vault')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment