python实现批量转换文件编码(批转换编码示例)

时间:2023-07-28 22:03:47 


# -*- 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):
        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)

标签:文件编码
0
投稿

猜你喜欢

  • Python模块汇总(常用第三方库)

    2023-05-21 16:25:37
  • python判断文件是否存在,不存在就创建一个的实例

    2022-04-29 02:28:55
  • MySQL数据库Shell import_table数据导入

    2024-01-15 02:34:55
  • 数据库清除日志文件(LDF文件过大)

    2024-01-14 04:05:36
  • 能否推荐一个论坛用的数据库结构?

    2009-11-01 18:09:00
  • SQL Server误区30日谈 第17天 有关页校验和的误区

    2024-01-27 17:48:47
  • 详解PHP的Sodium加密扩展函数

    2024-03-17 23:53:02
  • python实战练习做一个随机点名的程序

    2022-06-16 07:42:31
  • Python中通过@classmethod 实现多态的示例

    2021-02-25 06:10:33
  • Python完成毫秒级抢淘宝大单功能

    2023-09-29 04:14:54
  • Python爬取用户观影数据并分析用户与电影之间的隐藏信息!

    2022-09-09 23:19:47
  • Python数据可视化教程之Matplotlib实现各种图表实例

    2021-08-19 01:18:10
  • 用python实现批量重命名文件的代码

    2023-01-02 09:09:20
  • SQL Server常见问题及解决方法分享

    2024-01-15 01:45:30
  • javascript实现瀑布流自适应遇到的问题及解决方案

    2024-04-16 10:35:23
  • Kears+Opencv实现简单人脸识别

    2022-06-22 05:42:06
  • Python从视频中提取音频的操作

    2021-08-26 23:55:26
  • Python XlsxWriter模块Chart类用法实例分析

    2021-05-21 14:02:57
  • asp如何用JMail同时给多人发信?

    2010-06-12 12:52:00
  • python 实现任务管理清单案例

    2023-09-01 04:59:17
  • asp之家 网络编程 m.aspxhome.com