Force convert EUC-KR encoded files to UTF-8 and save in separate folder (WARNING: destructive)

WARNING: All files in the current directory will be forcefully converted to UTF-8 while ignoring errors. However, they will all be stored in a separate utf8 folder. Please don’t use this script unless you know what you’re doing. I’m providing it for convenience.

#!/usr/bin/python3
import os
from os import listdir, mkdir
from os.path import isfile, join

mypath = '.'

try:
    os.mkdir('utf8')
except:
    pass

onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))]

for f in onlyfiles:
    print('Converting %s -> utf8/%s' % (f, f))
    fd = open(f, 'r', encoding='euc-kr', errors='ignore')
    contents = fd.read()
    fd.close()

    fd = open(join(join(mypath, 'utf8'), f), 'w', encoding='utf-8')
    fd.write(contents)
    fd.close()

 

Leave a Reply

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