Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tigran123/aafee6f58295ce6e6c06af9eb0aab0fb to your computer and use it in GitHub Desktop.
Save tigran123/aafee6f58295ce6e6c06af9eb0aab0fb to your computer and use it in GitHub Desktop.
Cyrillic to Latin File Changer
#!/usr/bin/python
# -*- coding: utf-8 -*-
from os import walk, rename, mkdir
from os.path import isdir, exists
from sys import argv, exit, getfilesystemencoding
conversion = {
u'А': u'A',
u'Б': u'B',
u'В': u'V',
u'Г': u'G',
u'Д': u'D',
u'Е': u'E',
u'Ё': u'E',
u'Ж': u'Zh',
u'З': u'Z',
u'И': u'I',
u'Й': u'Y',
u'К': u'K',
u'Л': u'L',
u'М': u'M',
u'Н': u'N',
u'О': u'O',
u'П': u'P',
u'Р': u'R',
u'С': u'S',
u'Т': u'T',
u'У': u'U',
u'Ф': u'F',
u'Х': u'H',
u'Ц': u'Ts',
u'Ч': u'Ch',
u'Ш': u'Sh',
u'Щ': u'Sch',
u'Ъ': u'',
u'Ы': u'Y',
u'Ь': u'',
u'Э': u'E',
u'Ю': u'Ju',
u'Я': u'Ja',
u'а': u'a',
U'б': u'b',
u'в': u'v',
u'г': u'g',
u'д': u'd',
u'е': u'e',
u'ё': u'e',
u'ж': u'zh',
u'з': u'z',
u'и': u'i',
u'й': u'y',
u'к': u'k',
u'л': u'l',
u'м': u'm',
u'н': u'n',
u'о': u'o',
u'п': u'p',
u'р': u'r',
u'с': u's',
u'т': u't',
u'у': u'u',
u'ф': u'f',
u'х': u'h',
u'ц': u'ts',
u'ч': u'ch',
u'ш': u'sh',
u'щ': u'sch',
u'ъ': u'',
u'ы': u'y',
u'ь': u'',
u'э': u'e',
u'ю': u'ju',
u'я': u'ja',
u' ' : '-',
}
def cyr2lat(s):
retval = ""
for c in s:
if ord(c) > 128:
try:
c = conversion[c]
except KeyError:
c=''
elif c == ' ':
c = '-'
retval += c
return retval
if len(argv) == 1:
print "Usage: %s <dirs>" % argv[0]
exit(-1)
processed = []
def recursive_walk(dir):
# See http://docs.activestate.com/activepython/2.5/whatsnew/2.3/node6.html
found = []
dir = unicode(dir)
for finfo in walk(dir, True):
dirnames = finfo[1]
fnames = finfo[2]
for subdir in dirnames:
subdir = "%s/%s" % (dir, subdir)
if subdir in processed:
continue
for yield_val in recursive_walk(subdir):
yield yield_val
for fname in fnames:
yield '%s/%s' % (dir, fname)
raise StopIteration
if __name__ == "__main__":
fs_enc = getfilesystemencoding()
for dir in argv[1:]:
for fpath in recursive_walk(dir):
new_fpath = cyr2lat(fpath)
print 'new path %s' % new_fpath
print fpath.encode('utf-8')
# First make dirs
path_elts = new_fpath.split('/')
for idx in range(len(path_elts))[1:]:
subpath = '/'.join(path_elts[:idx])
while True:
i = 0
if exists(subpath):
if not isdir(subpath):
print '%s exists but is not a directory, will try again' % subpath
subpath += str(i)
continue
else:
path_elts[idx - 1] = subpath.split('/')[-1]
break
else:
print 'Creating directory: %s' % subpath
mkdir(subpath)
break
print 'Renaming %s to %s' % (fpath, new_fpath)
rename(fpath, new_fpath)
@a2nt
Copy link

a2nt commented Feb 6, 2018

Music /usr/local/bin/cyr2lat-filename.sh /home/tony/Music
new path /home/tony/Music/()/-/16..mp3
/home/tony/Music/
(
)/
__-/16..mp3
Creating directory:
Traceback (most recent call last):
File "/usr/local/bin/cyr2lat-filename.sh", line 137, in
mkdir(subpath)
OSError: [Errno 2] No such file or directory: ''

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment