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实现获取网站PR及百度权重
Jan 21 Python
Python实现微信公众平台自定义菜单实例
Mar 20 Python
自己编程中遇到的Python错误和解决方法汇总整理
Jun 03 Python
Python3如何解决字符编码问题详解
Apr 23 Python
python urllib urlopen()对象方法/代理的补充说明
Jun 29 Python
python爬虫使用cookie登录详解
Dec 27 Python
urllib和BeautifulSoup爬取维基百科的词条简单实例
Jan 17 Python
python批量替换页眉页脚实例代码
Jan 22 Python
Python星号*与**用法分析
Feb 02 Python
python简单验证码识别的实现方法
May 10 Python
python3 写一个WAV音频文件播放器的代码
Sep 27 Python
在pycharm中实现删除bookmark
Feb 14 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中获取内网用户MAC地址(WINDOWS/linux)的实现代码
2011/08/11 PHP
PHP中strtr字符串替换用法详解
2014/11/26 PHP
PHP实现的mysql主从数据库状态检测功能示例
2017/07/20 PHP
Nginx下ThinkPHP5的配置方法详解
2017/08/01 PHP
PHP操作Redis数据库常用方法示例
2018/08/25 PHP
isArray()函数(JavaScript中对象类型判断的几种方法)
2009/11/26 Javascript
jquery中ajax调用json数据的使用说明
2011/03/17 Javascript
jquery选择器的选择使用及性能介绍
2013/01/16 Javascript
js中的this关键字详解
2013/09/25 Javascript
Js可拖拽放大的层拖动特效实现方法
2015/02/25 Javascript
javascript背景时钟实现方法
2015/06/18 Javascript
jQuery禁用键盘后退屏蔽F5刷新及禁用右键单击
2016/01/22 Javascript
AngularJS实现标签页的两种方式
2016/09/05 Javascript
js+html5实现侧滑页面效果
2017/07/15 Javascript
angular2中Http请求原理与用法详解
2018/01/11 Javascript
jQuery实现动态添加和删除input框实例代码
2019/03/26 jQuery
小程序实现搜索框功能
2020/03/26 Javascript
layui对工具条进行选择性的显示方法
2019/09/19 Javascript
微信小程序如何实现点击图片放大功能
2020/01/21 Javascript
python TKinter获取文本框内容的方法
2018/10/11 Python
Python反爬虫技术之防止IP地址被封杀的讲解
2019/01/09 Python
对Python中的条件判断、循环以及循环的终止方法详解
2019/02/08 Python
pygame实现五子棋游戏
2019/10/29 Python
Python控制台输出时刷新当前行内容而不是输出新行的实现
2020/02/21 Python
完美解决ARIMA模型中plot_acf画不出图的问题
2020/06/04 Python
详解Python模块化编程与装饰器
2021/01/16 Python
JustFab加拿大:女鞋、靴子、手袋和服装在线
2018/05/18 全球购物
C# .NET面试题
2015/11/28 面试题
会计主管岗位职责
2014/01/03 职场文书
信息专业毕业生五年职业规划参考
2014/02/06 职场文书
运动会入场词50字
2014/02/20 职场文书
《猫》教学反思
2014/02/26 职场文书
募捐倡议书
2014/04/14 职场文书
服务承诺书怎么写
2014/05/24 职场文书
电气自动化求职信
2014/06/24 职场文书
javascript遍历对象的五种方式实例代码
2021/10/24 Javascript