[도쿠위키] 파일명 인코딩을 urlencode 에서 utf8로 변경하기
UTF-8 로 변경
그리하여, 저장된 파일명들을 모두 utf-8로 변경하기로 합니다.
도쿠위키에서 파일들은 /data/pages
밑에 저장됩니다.
- snippet.python
import os from glob import glob import urllib.parse def change_file(path): files = glob(path, recursive=True) for file in files: if os.path.isfile(file): dir_name = os.path.dirname(file) file_name = os.path.basename(file) print('{} and {}'.format(dir_name, file_name)) new_name = urllib.parse.unquote(file_name) print("encode to {}".format(new_name)) os.rename(os.path.join(dir_name, file_name), os.path.join(dir_name, new_name)) def change_folder(path): files = glob(path, recursive=True) for file in files: if os.path.isdir(file): new_name = urllib.parse.unquote(file) print("encode to {}".format(new_name)) os.rename(file, new_name) if __name__ == "__main__": base_folder = '/YOUR_PATH/pages' change_file(base_folder + '/**/*.txt') change_folder(base_folder + '/**/')
디렉토리 구조가 깊게 되어 있다면 폴더명 변경시에 해당 디렉토리가 변경되므로, 폴더명 변경을 여러번 해야 합니다.
에러 처리
You should check your config and permission settings.
에러 발생시 폴더에 권한을 부여합니다.
- snippet.shell
chmod -R 777 data/ ; chmod -R 777 lib/ ; chmod -R 777 conf/
- snippet.shell
sudo chgrp -R www-data ./data sudo chgrp -R www-data ./lib sudo chgrp -R www-data ./conf