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 相关文章推荐
记录Django开发心得
Jul 16 Python
Python过滤列表用法实例分析
Apr 29 Python
python遍历 truple list dictionary的几种方法总结
Sep 11 Python
python实现requests发送/上传多个文件的示例
Jun 04 Python
TensorFlow的权值更新方法
Jun 14 Python
python中for循环把字符串或者字典添加到列表的方法
Jul 20 Python
Python进度条的制作代码实例
Aug 31 Python
wxPython实现分隔窗口
Nov 19 Python
python进程的状态、创建及使用方法详解
Dec 06 Python
matplotlib 对坐标的控制,加图例注释的操作
Apr 17 Python
通俗易懂了解Python装饰器原理
Sep 17 Python
python实现三种随机请求头方式
Jan 05 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
真正的ZIP文件操作类(php)
2007/07/21 PHP
PHP计算日期相差天数实例分析
2016/02/23 PHP
php获取一定范围内取N个不重复的随机数
2016/05/28 PHP
PHP生成制作验证码的简单实例
2016/06/12 PHP
php基于闭包实现函数的自调用(递归)实例分析
2016/11/11 PHP
PHP编程获取各个时间段具体时间的方法
2017/05/26 PHP
PHP7 mongoDB扩展使用的方法分享
2019/05/02 PHP
把文本中的URL地址转换为可点击链接的JavaScript、PHP自定义函数
2014/07/29 Javascript
60个很实用的jQuery代码开发技巧收集
2014/12/15 Javascript
jQuery学习笔记之基础中的基础
2015/01/19 Javascript
基于Jquery实现焦点图淡出淡入效果
2015/11/30 Javascript
Javascript将数值转换为金额格式(分隔千分位和自动增加小数点)
2016/06/22 Javascript
js改变透明度实现轮播图的算法
2020/08/24 Javascript
基于vue实现分页/翻页组件paginator示例
2017/03/09 Javascript
jQuery日程管理控件glDatePicker用法详解
2017/03/29 jQuery
微信小程序 商城开发(ecshop )简单实例
2017/04/07 Javascript
解决Layui数据表格的宽高问题
2019/09/28 Javascript
Vue检测屏幕变化来改变不同的charts样式实例
2020/10/26 Javascript
[46:43]DOTA2上海特级锦标赛主赛事日 - 1 胜者组第一轮#2LGD VS MVP.Phx第二局
2016/03/02 DOTA
Python中的错误和异常处理简单操作示例【try-except用法】
2017/07/25 Python
python正则实现计算器功能
2017/12/14 Python
python高级特性和高阶函数及使用详解
2018/10/17 Python
Python实现读取txt文件中的数据并绘制出图形操作示例
2019/02/26 Python
深入浅析Python 命令行模块 Click
2020/03/11 Python
Window版下在Jupyter中编写TensorFlow的环境搭建
2020/04/10 Python
六种酷炫Python运行进度条效果的实现代码
2020/07/17 Python
python 如何使用find和find_all爬虫、找文本的实现
2020/10/16 Python
乌克兰香水和化妆品网站:Notino.ua
2018/03/26 全球购物
劳力士官方珠宝商:J.R. Dunn Jewelers
2018/09/29 全球购物
测绘工程个人的自我评价
2013/11/23 职场文书
《黄河颂》教学反思
2014/02/07 职场文书
党的群众路线学习笔记
2014/11/06 职场文书
2014年工程工作总结
2014/11/25 职场文书
2015年教师节演讲稿范文
2015/03/19 职场文书
趣味运动会加油词
2015/07/18 职场文书
党章学习心得体会2016
2016/01/14 职场文书