Skip to content

Instantly share code, notes, and snippets.

@FilipDominec
Created June 9, 2017 13:25
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 FilipDominec/d6d19986c92a89a594902cb3d4aa1c05 to your computer and use it in GitHub Desktop.
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
#!/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