python批量修改编码

#!/usr/bin/env python
# coding : utf-8

import os,sys  
import re;

def convert( filename, in_enc = "gbk", out_enc="utf-8" ):  
    try:  
        p = re.search(r'cpp' , filename); #添加过滤条件
        if p == None:
            return;
        print ("convert " + filename)  
        content = open(filename).read()  
        new_content = content.decode(in_enc).encode(out_enc)  
        open(filename, 'w').write(new_content)  
        print (" done")  
    except:  
        print (" error" ) 
  
def explore(dir):  
    for root, dirs, files in os.walk(dir):  
        for file in files:  
            path = os.path.join(root, file)  
            convert(path)  
  
def main():  
    for path in sys.argv[1:]:  
        if os.path.isfile(path):  
            convert(path)  
        elif os.path.isdir(path):  
            explore(path)  
  
if __name__ == "__main__":  
    main()  


这个是递归的,原理还不太懂,貌似python2的一个decoding 一个encoding 就完事了,应该是decoding变成了 Unicode,然后有encoding成utf-8了


你可能感兴趣的:(python)