SoFunction
Updated on 2024-12-10

python realize batch conversion file encoding (batch conversion encoding example)


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

import os
import glob

class Encoding:
    def __init__(self):
# file extension
        = ".*"
# encoding
        =None
        =None

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

    def processDirectory(self, args, dirname, filenames):
        print 'Directory', dirname
        for filename in filenames:
            if not (dirname+'/'+filename):
                if () or == ".*":
                    print ' File', filename
                    self.f2f(dirname+'/'+filename)

    def f2f(self, filepath, srcEncoding=None, dstEncoding=None):
        try:
            f1 = open(filepath, 'rb')
            temp = ()
            ()
            f2 = open(filepath, 'wb')
            (().encode())
            ()
print 'Transcoded successfully'
        except Exception, e:
            print e


    def colectFileType(self, dirname, fileType):
        for filename in (r'*.'+fileType):
            print filename

    def setExt(self, ext):
        if not ('.'):
            ext = "." + ext
        = ext

    def setSRC(self, encoding):
        =encoding

    def setDST(self, encoding):
        =encoding

if __name__ == '__main__':
    obj = Encoding()
print u'Please enter the file type:'
    (raw_input())
print u'Please enter the original file encoding:'
    (raw_input())
print u'Please enter the file target type:'
    (raw_input())
    """('html')
    ('gbk')
    ('utf-8')"""
print u'Please enter the directory where the file is located:'
    path = raw_input()
    (path, , None)