最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python實(shí)現(xiàn)批量轉(zhuǎn)換文件編碼(批轉(zhuǎn)換編碼示例)

 更新時間:2014年01月23日 09:52:28   作者:  
這篇文章主要介紹了python實(shí)現(xiàn)批量轉(zhuǎn)換文件編碼示例,指定文件編碼、目錄或擴(kuò)展名即可進(jìn)行轉(zhuǎn)換,大家參考使用吧

復(fù)制代碼 代碼如下:

# -*- coding:utf-8 -*-
__author__ = 'walkskyer'

import os
import glob

class Encoding:
    def __init__(self):
        #文件擴(kuò)展名
        self.ext = ".*"
        #編碼
        self.srcEncoding=None
        self.dstEncoding=None

    def convertEncoding(self, content, srcEncoding=None, dstEncoding=None):
        return content.decode(self.srcEncoding).encode(self.dstEncoding)

    def processDirectory(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=None, 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 '轉(zhuǎn)碼成功'
        except Exception, e:
            print e


    def colectFileType(self, dirname, fileType):
        for filename 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=encoding

if __name__ == '__main__':
    obj = Encoding()
    print u'請輸入文件類型:'
    obj.setExt(raw_input())
    print u'請輸入文件原始編碼:'
    obj.setSRC(raw_input())
    print u'請輸入文件目標(biāo)類型:'
    obj.setDST(raw_input())
    """obj.setExt('html')
    obj.setSRC('gbk')
    obj.setDST('utf-8')"""
    print u'請輸入文件所在目錄:'
    path = raw_input()
    os.path.walk(path, obj.processDirectory, None)

相關(guān)文章

最新評論

治多县| 四平市| 德令哈市| 新巴尔虎左旗| 三穗县| 东辽县| 桦南县| 西昌市| 镇坪县| 垦利县| 青阳县| 淮北市| 营口市| 健康| 那曲县| 合江县| 惠州市| 宜丰县| 瓦房店市| 奎屯市| 南部县| 南丰县| 阿坝| 波密县| 霞浦县| 介休市| 荔波县| 佳木斯市| 交口县| 清远市| 公主岭市| 邢台市| 五华县| 尚义县| 昌邑市| 沙田区| 大余县| 安塞县| 嫩江县| 郸城县| 尼玛县|