Last active
March 15, 2024 06:18
-
-
Save fagci/a4860cf6a9659cc10724bddf6d8c4e74 to your computer and use it in GitHub Desktop.
Transliterate files and directories names
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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