Fix broken Korean filenames

Fix broken Korean filenames (e.g., Áö´ÉÇüÇüżҺм®±â.zip -> 지능형형태소분석기.zip)

Fixes all filenames in current directory

#!/usr/bin/python3
from os import listdir, rename
from os.path import isfile, isdir, join, exists

mypath = '.'
onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f)) or isdir(join(mypath, f))]

for f in onlyfiles:
    origF = f
    try:
        # iso-8859-1 is the secret...
        f = f.encode('iso-8859-1')
        #if f.decode('iso-8859-1').endswith('.doc'):

        # 깨짐 detection
        fixedFn = f.decode('euc-kr', 'ignore')
        print(fixedFn)

        if(not exists(fixedFn)):
            print('%s -> %s' % (origF, fixedFn))
            rename(origF, fixedFn)
        else:
            print('%s -> %s (X: already exists)' % (origF, fixedFn))
    except:
        print('Failed: %s' % origF)

 

Leave a Reply

Your email address will not be published. Required fields are marked *