Skip to content

Instantly share code, notes, and snippets.

@andreyshuster
Created September 18, 2022 17:36
Show Gist options
  • Save andreyshuster/5b9831005358cbc2739be885eb579e21 to your computer and use it in GitHub Desktop.
Save andreyshuster/5b9831005358cbc2739be885eb579e21 to your computer and use it in GitHub Desktop.
convert cyrillic to latin in filenames
#!/usr/bin/env python
import os
from shutil import move
translation_table = {
"А": "A",
"а": "a",
"Б": "B",
"б": "b",
"В": "V",
"в": "v",
"Г": "G",
"г": "g",
"Д": "D",
"д": "d",
"Е": "E",
"е": "e",
"Ё": "Yo",
"ё": "yo",
"Ж": "Zh",
"ж": "zh",
"З": "Z",
"з": "z",
"И": "I",
"и": "i",
"Й": "Y",
"й": "y",
"К": "K",
"к": "k",
"Л": "L",
"л": "l",
"М": "M",
"м": "m",
"Н": "N",
"н": "n",
"О": "O",
"о": "o",
"П": "P",
"п": "p",
"Р": "R",
"р": "r",
"С": "S",
"с": "s",
"Т": "T",
"т": "t",
"У": "U",
"у": "u",
"Ф": "F",
"ф": "f",
"Х": "Kh",
"х": "kh",
"Ц": "Ts",
"ц": "ts",
"Ч": "Ch",
"ч": "ch",
"Ш": "Sh",
"ш": "sh",
"Щ": "Shch",
"щ": "shch",
"Ъ": "ʺ",
"ъ": "ʺ",
"Ы": "Y",
"ы": "y",
"Ь": "'",
"ь": "'",
"Э": "E",
"э": "e",
"Ю": "Yu",
"ю": "yu",
"Я": "Ya",
"я": "ya",
"—": "-",
}
def transliterate(fname):
return "".join([
translation_table[ch]
if ch in translation_table.keys()
else ch
for ch in fname
])
def main():
[
#print(f"{transliterate(f)}")
move(f, f"{transliterate(f)}")
for f in os.listdir('.')
if not f.startswith('.')
]
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment