Python实现批量转换文件编码的方法


Posted in Python onJuly 28, 2015

本文实例讲述了Python实现批量转换文件编码的方法。分享给大家供大家参考。具体如下:

这里将某个目录下的所有文件从一种编码转换为另一种编码,然后保存

import os
import shutil
def match(config,fullpath,type):
  flag=False
  if type == 'exclude':
    for item in config['src']['exclude']:
      if fullpath.startswith(config['src']['path']+os.path.sep+item):
        flag=True
        break
  if type=='filter':
    for item in config['src']['filter']:
      if fullpath.endswith(item):
        flag=True
        break
  return flag
def conver_file(param):
  for root, dirs, files in os.walk(param['src']['path']):
    for filename in files:
      readfile=root+os.path.sep+"%s" %filename
      print(readfile)
      if 'filter' in param['src']:
        if not (match(param,readfile,'filter')):
          continue
      s=''
      outfile=readfile.replace(param['src']['path'],param['dest']['path'])
      try :
        s=open(readfile,encoding=param['src']['encoding']).read()
      except:
        print("file %s read erro" % readfile)
        shutil.copy(readfile,outfile)
      if s: #False and
        print("save")
        with open(outfile, mode='w', encoding=param['dest']['encoding']) as a_file:
          a_file.write(s)
    for dirname in dirs:
      file=root+os.path.sep+"%s" %dirname
      if 'exclude' in param['src']:
        if(match(param,file,'exclude')):
          continue
      outdir=file.replace(param['src']['path'],param['dest']['path'])
      #print(outdir)
      if not os.path.isdir(outdir):
        os.mkdir(outdir)
if __name__ == "__main__":
  param={'src':{'path':r'D:\work\test\trunk','encoding':'gbk','exclude':['dataa'],'filter':['.php','.html','.htm']},
    'dest':{'path':"f:\\test\\new",'encoding':'utf-8'}}
  conver_file(param)

希望本文所述对大家的Python程序设计有所帮助。

Python 相关文章推荐
python 获取本机ip地址的两个方法
Feb 25 Python
Python编程实现使用线性回归预测数据
Dec 07 Python
unittest+coverage单元测试代码覆盖操作实例详解
Apr 04 Python
Python中将dataframe转换为字典的实例
Apr 13 Python
Python利用WMI实现ping命令的例子
Aug 14 Python
通过selenium抓取某东的TT购买记录并分析趋势过程解析
Aug 15 Python
python列表推导和生成器表达式知识点总结
Jan 10 Python
查看已安装tensorflow版本的方法示例
Apr 19 Python
Python使用Chrome插件实现爬虫过程图解
Jun 09 Python
详解python中的lambda与sorted函数
Sep 04 Python
pycharm最新激活码有效期至2100年(亲测可用)
Feb 05 Python
Python基础之字符串格式化详解
Apr 21 Python
Python中subprocess的简单使用示例
Jul 28 #Python
Python中文竖排显示的方法
Jul 28 #Python
Python中的getopt函数使用详解
Jul 28 #Python
Python3访问并下载网页内容的方法
Jul 28 #Python
python3抓取中文网页的方法
Jul 28 #Python
python列表操作之extend和append的区别实例分析
Jul 28 #Python
python创建列表并给列表赋初始值的方法
Jul 28 #Python
You might like
使用php验证复选框有效性的示例
2013/11/13 PHP
window.open不被拦截的实现代码
2012/08/22 Javascript
2012年开发人员的16款新鲜的jquery插件体验分享
2012/12/28 Javascript
jQuery Mobile 导航栏代码
2013/11/01 Javascript
弹出最简单的模式化遮罩层的js代码
2013/12/04 Javascript
JavaScript数据结构和算法之图和图算法
2015/02/11 Javascript
使用Node.js为其他程序编写扩展的基本方法
2015/06/23 Javascript
深入学习jQuery Validate表单验证(二)
2016/01/18 Javascript
AngularJS 实现JavaScript 动画效果详解
2016/09/08 Javascript
AngularJS 支付倒计时功能实现思路
2017/06/05 Javascript
vue使用v-for实现hover点击效果
2018/09/29 Javascript
浅谈Angular 观察者模式理解
2018/11/01 Javascript
微信小程序自定义toast弹窗效果的实现代码
2018/11/15 Javascript
jquery无缝图片轮播组件封装
2020/11/25 jQuery
JS实现瀑布流效果
2020/03/07 Javascript
JS自定义滚动条效果
2020/03/13 Javascript
解决Python获取字典dict中不存在的值时出错问题
2018/10/17 Python
对python中数据集划分函数StratifiedShuffleSplit的使用详解
2018/12/11 Python
用Python实现BP神经网络(附代码)
2019/07/10 Python
Python的matplotlib绘图如何修改背景颜色的实现
2019/07/16 Python
python实现大文本文件分割
2019/07/22 Python
Python super()函数使用及多重继承
2020/05/06 Python
如何让PyQt5中QWebEngineView与JavaScript交互
2020/10/21 Python
如何掌握自荐信格式呢
2013/11/19 职场文书
技术总监岗位职责
2013/12/05 职场文书
建筑总经理岗位职责
2014/02/02 职场文书
你的创业计划书怎样才能打动风投
2014/02/06 职场文书
销售团队激励口号
2014/06/06 职场文书
思想作风纪律整顿心得体会
2014/09/04 职场文书
大学生就业意向书
2015/05/11 职场文书
贷款担保书范本
2015/09/22 职场文书
详解Python牛顿插值法
2021/05/11 Python
Mysql数据库值的添加、修改、删除及清空操作实例
2021/06/20 MySQL
MySQL系列之十一 日志记录
2021/07/02 MySQL
一次Mysql update sql不当引起的生产故障记录
2022/04/01 MySQL
Golang 并发编程 SingleFlight模式
2022/04/26 Golang