python implements bulk conversion file encoding (Batch conversion encoding example)
#-*-Coding:utf-8-*-author = ' walkskyer ' import osimport globclass encoding:def init (self): #文件扩展名 self . ext = ". *" #编码 self.srcencoding=none self.dstencoding=none def convertencoding (self, content, SRC Encoding=none, Dstencoding=none): Return Content.decode (self.srcencoding). Encode (self.dstencoding) def processDi Rectory (self, args, dirname, filenames): print ' Directory ', dirname for filename in filenames:if Not Os.path.isdir (dirname+ '/' +filename): If Filename.endswith (self.ext) or Self.ext = = ". *": print ' File ', filename self.f2f (dirname+ '/' +filename) def f2f (self, filepath, Srcencoding=non E, dstencoding=none): try:f1 = open (filepath, ' RB ') temp = F1.read () f1.close () F2 = open (filepath, ' WB ') F2.write (Temp.decode (self.srcencoding). Encode (self.dstencoding)) F2.close () pRint ' transcoding success ' except Exception, E:print e def colectfiletype (self, dirname, FileType): For file Name in Glob.glob (R ' *. ') +filetype): print filename def setext (self, ext): If not Ext.startswith ('. '): ext = "." + Ext self.ext = ext def setsrc (self, Encoding): Self.srcencoding=encoding def setdst (self, Encoding): Self.dstencoding=encodingif name = = ' main ': obj = Encoding () print U ' Please enter file type: ' Obj.setext (Raw_input ()) p rint u ' input file original code: ' Obj.setsrc (Raw_input ()) Print U ' enter file target type: ' Obj.setdst (Raw_input ()) ' "" "Obj.setext (' HTML ') OBJ.SETSRC (' GBK ') obj.setdst (' Utf-8 ') "" "" Print U "Please enter the directory where the file is located: ' Path = Raw_input () os.path.walk (Path, Obj.proce Ssdirectory, None)
The above is the python implementation of the bulk conversion file encoding (Batch conversion encoding example) content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!