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字符串切片操作知识详解
Mar 28 Python
Python获取当前路径实现代码
May 08 Python
scrapy spider的几种爬取方式实例代码
Jan 25 Python
Tensorflow 同时载入多个模型的实例讲解
Jul 27 Python
python如何获取列表中每个元素的下标位置
Jul 01 Python
解决yum对python依赖版本问题
Jul 05 Python
Flask框架重定向,错误显示,Responses响应及Sessions会话操作示例
Aug 01 Python
django-filter和普通查询的例子
Aug 12 Python
Python Scrapy框架第一个入门程序示例
Feb 05 Python
python虚拟环境模块venv使用及示例
Mar 04 Python
Python 文本滚动播放器的实现代码
Apr 25 Python
Python anaconda安装库命令详解
Oct 16 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 批量删除数据的方法分析
2009/10/30 PHP
PHP 删除一个目录及目录下的所有文件的函数代码
2010/05/26 PHP
PHP 下载文件时如何自动添加bom头及解释BOM头和去掉bom头的方法
2016/01/04 PHP
php获取数据库中数据的实现方法
2017/06/01 PHP
PHP使用Redis实现Session共享的实现示例
2019/05/12 PHP
JavaScript 监听textarea中按键事件
2009/10/08 Javascript
Javascript var变量隐式声明方法
2009/10/19 Javascript
jQuery 1.5最新版本的改进细节分析
2011/01/19 Javascript
Javascript 赋值机制详解
2014/11/23 Javascript
详细分析JavaScript函数定义
2015/07/16 Javascript
js实现简单计算器
2015/11/22 Javascript
JS在浏览器中解析Base64编码图像
2017/02/09 Javascript
在 Node.js 中使用原生 ES 模块方法解析
2017/09/19 Javascript
解决vue项目打包后提示图片文件路径错误的问题
2018/07/04 Javascript
JS动画实现回调地狱promise的实例代码详解
2018/11/08 Javascript
vue.js仿hover效果的实现方法示例
2019/01/28 Javascript
JavaScript刷新页面的几种方法总结
2019/03/28 Javascript
微信小程序登录态和检验注册过没的app.js写法
2019/05/22 Javascript
layui递归实现动态左侧菜单
2019/07/26 Javascript
解决VUE mounted 钩子函数执行时 img 未加载导致页面布局的问题
2020/07/27 Javascript
[01:07:53]RNG vs VG 2019国际邀请赛小组赛 BO2 第一场 8.15
2019/08/17 DOTA
解决pyqt中ui编译成窗体.py中文乱码的问题
2016/12/23 Python
pygame游戏之旅 添加碰撞效果的方法
2018/11/20 Python
Python类中方法getitem和getattr详解
2019/08/30 Python
python3.x中安装web.py步骤方法
2020/06/23 Python
matplotlib之属性组合包(cycler)的使用
2021/02/24 Python
英国著名国际平价时尚男装品牌:Topman
2016/08/27 全球购物
美国二手奢侈品寄售网站:TheRealReal
2016/10/29 全球购物
Liu Jo西班牙官网:意大利服装品牌
2019/09/11 全球购物
新奥尔良珠宝:Mignon Faget
2020/11/23 全球购物
Electric官网:美国高级眼镜和配件品牌
2020/06/04 全球购物
Sql面试题
2013/03/20 面试题
出国留学自荐信
2013/10/25 职场文书
学生党员思想汇报
2013/12/28 职场文书
车辆年审委托书范本
2014/09/18 职场文书
win10搭建配置ftp服务器的方法
2022/08/05 Servers