File transcoding gadgets written in python

Source: Internet
Author: User
Sourceinsight does not support UTF-8. to support Chinese display, write a python applet to transcode all files in a directory from UTF-8 to gbk as follows :#! Usrlocalbinpython #-*-coding: UTF-8-*-sourceinsight does not support UTF-8. to support Chinese display, this program transcodes all files in a directory from UTF-8... source insight does not support UTF-8. to support Chinese display, write a python applet to transcode all files in a directory from UTF-8 to gbk.

The code is as follows:

#!/usr/local/bin/python# -*- coding: utf-8 -*-'''

Source insight does not support UTF-8. to support Chinese display, write this program.

Transcode all files in a directory from UTF-8 to gbk

usage: chmod +x convert.py./convert.py src mysrc'''import osimport shutilimport reimport 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)

The above is the content of the file transcoding tool written in python. For more information, see PHP Chinese network (www.php1.cn )!

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.