国产探花免费观看_亚洲丰满少妇自慰呻吟_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):

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 开远市| 玛曲县| 故城县| 甘肃省| 卢龙县| 宁明县| 高碑店市| 木里| 阜宁县| 丹寨县| 榆中县| 柳江县| 嘉禾县| 梨树县| 静乐县| 冕宁县| 西林县| 滨州市| 和静县| 东兰县| 碌曲县| 佛学| 阿巴嘎旗| 青海省| 右玉县| 崇州市| 合山市| 长葛市| 德阳市| 益阳市| 常德市| 景德镇市| 新泰市| 福建省| 馆陶县| 寿光市| 贺州市| 菏泽市| 漳平市| 洛阳市| 余干县|