자소변환

맥에서 사용하던 폴더나 파일을 윈도우로 가져오면 자음과 모음이 분리된다. 일명 자소분리라고 한다.

인터넷에서 다시 원래 이름으로 자음과 모음을 붙여 한 글자로 만들어주는 프로그램이 많이 있다. 그중 괜챦은  프로그램은 HangulJasoFixer2.exe 파일이다.

하지만, 파이썬으로도 간단하게 변환할 수 있다.

import os
import unicodedata

type = 'NFC'
stack = []
    
def findfile(path):
    for dirpath, dirname, filename in os.walk(path):
        
        for name in dirname:
            convertname = unicodedata.normalize(type, name)
            if convertname != name:
                #print(os.path.join(dirpath, name) + " -> " + os.path.join(dirpath, convertname))
                stack.append([os.path.join(dirpath, name), os.path.join(dirpath, convertname)])
    
        for name in filename:
            convertname = unicodedata.normalize(type, name)
            if convertname != name:
                #print(os.path.join(dirpath, name) + " -> " + os.path.join(dirpath, convertname))
                stack.append([os.path.join(dirpath, name), os.path.join(dirpath, convertname)])


def renamefile():
    print("result:")
    for k in range(len(stack)):
        files = stack.pop()
        print(files[0] + " --> " + files[1])
        os.rename(files[0], files[1])
        
findfile("c:\\temp")
renamefile()

 

위의 예제는 c:\temp 폴더 하위에 있는 모든 폴더나 파일의 자소변환을 해주는 것이다.

Leave a Comment

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다

이 사이트는 Akismet을 사용하여 스팸을 줄입니다. 댓글 데이터가 어떻게 처리되는지 알아보세요.