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 相关文章推荐
Python3读取文件常用方法实例分析
May 22 Python
详解appium+python 启动一个app步骤
Dec 20 Python
TensorFlow高效读取数据的方法示例
Feb 06 Python
Python文件常见操作实例分析【读写、遍历】
Dec 10 Python
详解django中url路由配置及渲染方式
Feb 25 Python
Spring Cloud Feign高级应用实例详解
Dec 10 Python
PyTorch学习:动态图和静态图的例子
Jan 06 Python
Python识别html主要文本框过程解析
Feb 18 Python
Python requests获取网页常用方法解析
Feb 20 Python
PyCharm 无法 import pandas 程序卡住的解决方式
Mar 09 Python
Python何绘制带有背景色块的折线图
Apr 23 Python
Python 使用 Frame tkraise() 方法在 Tkinter 应用程序中的Frame之间切换
Apr 24 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 base64编码后解码乱码的解决办法
2014/06/19 PHP
yii,CI,yaf框架+smarty模板使用方法
2015/12/29 PHP
PHP实现支持加盐的图片加密解密
2016/09/09 PHP
PHP 二维关联数组根据其中一个字段排序(推荐)
2017/04/04 PHP
php原生数据库分页的代码实例
2019/02/18 PHP
php定期拉取数据对比方法实例
2019/09/22 PHP
Javascript中Eval函数的使用说明
2008/10/11 Javascript
将nodejs打包工具整合到鼠标右键的方法
2013/05/11 NodeJs
自己编写的类似JS的trim方法
2013/10/09 Javascript
jquery中的on方法使用介绍
2013/12/29 Javascript
js使用html()或text()方法获取设置p标签的显示的值
2014/08/01 Javascript
深入理解JavaScript系列(47):对象创建模式(上篇)
2015/03/04 Javascript
js计算系统当前日期是星期几的方法
2016/07/14 Javascript
JavaScript中ES6 Babel正确安装过程
2016/07/18 Javascript
jQuery操作cookie
2016/08/08 Javascript
详解本地Node.js服务器作为api服务器的解决办法
2017/02/28 Javascript
JavaScript限定范围拖拽及自定义滚动条应用(3)
2017/05/17 Javascript
详谈js中标准for循环与foreach(for in)的区别
2017/11/02 Javascript
Node.js 中使用 async 函数的方法
2017/11/20 Javascript
JS 中document.write()的用法和清空的原因浅析
2017/12/04 Javascript
vue中的适配px2rem示例代码
2018/11/19 Javascript
浅谈Vue CLI 3结合Lerna进行UI框架设计
2019/04/14 Javascript
antd的select下拉框因为数据量太大造成卡顿的解决方式
2020/10/31 Javascript
Python使用迭代器捕获Generator返回值的方法
2017/04/05 Python
python计算日期之间的放假日期
2018/06/05 Python
详解Python 多线程 Timer定时器/延迟执行、Event事件
2019/06/27 Python
Python 批量刷博客园访问量脚本过程解析
2019/08/30 Python
Python实现线性插值和三次样条插值的示例代码
2019/11/13 Python
时尚圣经:The Fashion Bible
2019/03/03 全球购物
捐款倡议书范文
2014/02/02 职场文书
会计专业自我评价
2014/02/12 职场文书
汽修专业自荐信
2014/07/07 职场文书
年终晚会活动方案
2014/08/21 职场文书
食品质检员岗位职责
2015/04/08 职场文书
2015年秋季校长开学典礼致辞
2015/07/29 职场文书
2016年春季开学典礼新闻稿
2015/11/25 职场文书