Created
June 9, 2017 13:25
-
-
Save FilipDominec/d6d19986c92a89a594902cb3d4aa1c05 to your computer and use it in GitHub Desktop.
(not fully working) Manual character replacement in file names - if "chardet" and "convmv" fail
This file contains hidden or 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 | |
#-*- coding: utf-8 -*- | |
import os, sys | |
#import fileinput | |
AAA, BBB = u'¬ ì¡ ýØçæ§£§ççå¬ Øý', u'éčůčýíář욊žÚčžššňČěřň' | |
filelist = [os.path.join(dp, f) for dp, dn, fn in os.walk('.') for f in fn] | |
filelist.sort() | |
for line in filelist : | |
# os.listdir(u'.'): | |
print('OLD = ', line.encode('utf-8', 'surrogateescape').decode('ISO-8859-1')) | |
newfix = [] | |
for j,c in enumerate(line): | |
rr = False | |
for f,t in zip(AAA, BBB): ## all replacements | |
#fix = fix.replace(f.encode('utf-8', 'surrogateescape').decode('ISO-8859-1'), t.encode('utf-8', 'surrogateescape').decode('ISO-8859-1')) | |
#print(" ", f.encode('utf-8', 'surrogateescape')[-1:]==c.encode('utf-8', 'surrogateescape'), f.encode('utf-8', 'surrogateescape')[-1:], c.encode('utf-8', 'surrogateescape'),t, newfix) | |
if f.encode('utf-8', 'surrogateescape')[-1:]==c.encode('utf-8', 'surrogateescape'): | |
newfix += [t] | |
#print("REPLACING", f.encode('utf-8', 'surrogateescape')[-1:], c.encode('utf-8', 'surrogateescape')) | |
rr = True | |
#break | |
if not rr: | |
newfix += [c.encode('utf-8', 'surrogateescape').decode('ISO-8859-1')] ## [b'Z', 'á', b'z', b'n', ... | |
#print(newfix) | |
print(' NEW = ', ''.join(newfix)) | |
os.rename(line.encode('utf-8', 'surrogateescape').decode('ISO-8859-1'), ''.join(newfix)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment