import os import sys import codecs import chardet def convert(filename,out_enc="utf-8"): try: content=codecs.open(filename,'rb').read() source_encoding=chardet.detect(content)['encoding'] content=content.decode(source_encoding).encode(out_enc) content=content.replace(b'\r\n',b'\n') codecs.open(filename,'wb').write(content) print("Encoding file :{0}, convert from {1} to {2}".format(filename,source_encoding,out_enc)) except IOError as err: print("I/O error:{0}".format(err)) def explore(dir): suff_list=['.py','.txt','.sql'] for root,dirs,files in os.walk(dir): for file in files: suffix=os.path.splitext(file)[-1] if suffix.lower() in suff_list: path=os.path.join(root,file) convert(path) if __name__=="__main__": # dir="./test" dir=os.getcwd() if len(sys.argv)==2: dir=sys.argv[1] explore(dir)