#!/usr/local/bin/python
# -*- coding: utf-8 -*-
'''
source insight竟然不支持utf-8,为支持中文显示,故写该程序
将某个目录下所有文件全部从utf-8转码为gbk
usage: chmod +x convert.py
./convert.py src mysrc
'''
import os
import shutil
import re
import sys
#####################################
def search(src,dest,handler):
filelist=os.listdir(src)
for f in filelist:
cf=src+'/'+f
df=dest+'/'+f
if os.path.isdir(cf):
if not os.path.exists(df):
os.makedirs(df)
search(cf,df,handler)
else:
handler(cf,df)#the function dealing with file
##########################################################
def convertFile(sfn,dfn):
sf=open(sfn,'r')
s=sf.read()
try:
s=s.decode('utf-8')
df=open(dfn,'w')
df.write(s.encode('gbk'))
df.flush()
sf.close()
df.close()
print '*** ',sfn
except:
sf.close()
print sfn
sys.exit()
if __name__ == '__main__':
srcDir=os.getcwd()+'/'+sys.argv[1]
dstDir=os.getcwd()+'/'+sys.argv[2]
search(srcDir,dstDir,convertFile)