Python implements bulk conversion file encoding (Batch conversion encoding example)

Source: Internet
Author: User
Tags glob
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)!

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.