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

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

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

復制代碼 代碼如下:

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

import os
import glob

class Encoding:
    def __init__(self):
        #文件擴展名
        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'請輸入文件目標類型:'
    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)文章

最新評論

高碑店市| 北辰区| 宁安市| 茶陵县| 乌拉特后旗| 宝兴县| 永顺县| 青田县| 五寨县| 团风县| 抚松县| 玉树县| 繁昌县| 车险| 拉萨市| 宾川县| 铁岭市| 越西县| 大丰市| 东兰县| 华安县| 鄂州市| 祥云县| 高陵县| 清水河县| 武城县| 定安县| 马公市| 临汾市| 友谊县| 武汉市| 萝北县| 宝应县| 新营市| 图木舒克市| 吴江市| 滦平县| 河西区| 张北县| 甘德县| 息烽县|