国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python實現批量轉換文件編碼(批轉換編碼示例)

2020-02-23 05:06:54
字體:
來源:轉載
供稿:網友

代碼如下:
# -*- 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 '轉碼成功'
        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):

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 织金县| 景德镇市| 嘉义县| 英吉沙县| 河间市| 皮山县| 永登县| 福泉市| 锡林浩特市| 安义县| 武穴市| 来凤县| 灵宝市| 济阳县| 贺兰县| 西宁市| 连云港市| 青冈县| 拉萨市| 郎溪县| 张掖市| 厦门市| 英山县| 邵东县| 山阳县| 娱乐| 绩溪县| 嘉定区| 阿城市| 富裕县| 碌曲县| 九龙坡区| 广安市| 北海市| 新干县| 蓬安县| 合阳县| 车致| 和静县| 汽车| 海淀区|