Skip to content

Instantly share code, notes, and snippets.

@fagci
Last active March 15, 2024 06:18
Show Gist options
  • Save fagci/a4860cf6a9659cc10724bddf6d8c4e74 to your computer and use it in GitHub Desktop.
Save fagci/a4860cf6a9659cc10724bddf6d8c4e74 to your computer and use it in GitHub Desktop.
Transliterate files and directories names
#!/usr/bin/env python3
import argparse
from os import listdir, rename
from os.path import isfile, join
from re import sub
parser = argparse.ArgumentParser(prog='Transliterator', description='Transliterates file/dir names')
parser.add_argument('-f', '--force', action='store_true')
force = parser.parse_args().force
SYMBOLS = (u"абвгдеёжзийклмнопрстуфхцчшщъыьэюяАБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ ",
u"abvgdeejzijklmnoprstufhzcss_y_euaABVGDEEJZIJKLMNOPRSTUFHZCSS_Y_EUA_")
tr = {ord(a):ord(b) for a, b in zip(*SYMBOLS)}
for fo in listdir('.'):
fn = fo.translate(tr)
fn = sub('[^a-zA-Z0-9_.]', '', fn)
if force:
rename(fo, fn)
else:
print(fo, '=>', fn)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment