批量转换文本文件编码

批量转换文本文件编码
#version 2.0 # updated by Adam on 2005-10-31 #version 1.0 # updated by Adam on 2005-9-29 import os #import glob import string from os.path import * def Adjustcodec(orig,targetcode,oriname,targetname): try: inf=open(oriname) try: im = inf.read().decode(orig) inf.close() im2 = im.encode(targetcode) jb = open(targetname,'w') jb.write(im2) jb.close() except: pass except: print oriname def processFiles(SFilepath,DFilepath): global root,dirs,files import re s1=len(SourcePath) for root, dirs, files in os.walk(SFilepath): if len(files)!=0: for name in files: if name[-4:]!=".txt" : continue #fetch the file path info suffix=name.split('.')[-1] pic=os.path.join(root, name) temp=pic[s1+1:] ts=string.split(temp,'.') temp1=ts[0] # replace more than one spaces using '_' in filename temp2=re.sub('\s+|-+','_',temp1) # add filename suffix temp3=temp2 + r'.' + suffix #group the full destination path of file Dpic=os.path.join(DestPath, temp3) if os.path.isdir(DFilepath): pass else: os.makedirs(DFilepath) try: Adjustcodec("mbcs","utf-8",pic,Dpic) # store the big photo except: continue if len(dirs)!=0: #obey the source directory tree to build destination directory tree for dr in dirs: SFilepath=os.path.join(root, dr) temp=SFilepath[s1+1:] DFilepath=os.path.join(DestPath, temp) if os.path.isdir(DFilepath): pass else: os.makedirs(DFilepath) if __name__ == '__main__': SourcePath=r'E:\test' DestPath=r'E:\test2' maxsize=180 os.chdir(SourcePath) SFilepath=SourcePath DFilepath=DestPath processFiles(SFilepath,DFilepath)
设置