Python 转换某个目录下面的文本文件的编码 @for&ever 2009-10-23 以下代码用来转换某个目录下面的所有htm文件编码,由gbk到 utf-8 。 import sys import string import codecs import os import shutil def gbkToUtf8(path): files = os.listdir(path + "/gbk/") for filename in files: fileGBK = path + '/gbk/' + filename fileUTF = path + '/utf/' + filename #print fileGBK if os.path.isdir(fileGBK): gbkToUtf8(fileGBK) continue print 'Encode Converting (GBK to UTF-8) : ', fileGBK try: tokens = string.splitfields(filename, '.') if len(tokens) != 2 or tokens[1] != 'htm': #print tokens[1] shutil.copy(fileGBK, fileUTF) else: str = open(fileGBK).read().decode("gbk") str = str.encode("UTF-8") utfFile = open(fileUTF, 'w') utfFile.write(str) utfFile.close() except: print "error" if __name__ == "__main__": gbkToUtf8("E:/book-doc/") @forandever 2009-10-23