Created
August 12, 2014 15:31
-
-
Save Dollyn/d467bcb41700086a8000 to your computer and use it in GitHub Desktop.
Convert files to utf-8 encoding
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
import logging | |
import os | |
def convert(): | |
parser = argparse.ArgumentParser(description='Convert files in folders charset to utf-8') | |
parser.add_argument('path', help='folder path') | |
parser.add_argument('-s', nargs='?', default='GBK', help='source encoding') | |
args = parser.parse_args() | |
path = args.path | |
if os.path.isdir(path): | |
for root, dirs, files in os.walk(path, topdown=True): | |
for f in files: | |
sub_path = os.path.join(root, f) | |
src_file = open(sub_path, 'rb') | |
src = unicode(src_file.read(), 'GBK') | |
src_file.close() | |
dest_file = open(sub_path, 'w') | |
dest_file.write(src.encode('utf-8')) | |
dest_file.close() | |
else: | |
logging.error('%s is not a directory.') | |
if __name__ == "__main__": | |
logging.basicConfig(level=logging.INFO) | |
convert() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment