Python批量转换文件编码格式


Posted in Python onMay 17, 2015

自己写的方法,适用于linux,

#!/usr/bin/python
#coding=utf-8
import sys
import os, os.path
import dircache
import commands
def add(x,y):
 return x*y

def trans(dirname):
 lis = dircache.opendir(dirname)
 for a in lis:
af=dirname+os.sep+a
## print af
 if os.path.isdir(af):
## print af
trans(af)
else:
 ## print af+"encoding="+fi.name
 ft = commands.getoutput('file -i '+af)
## print ft
 if a.find('.htm')==-1 and a.find('.xml')==-1 and ft.find('text/')!=-1 and ft.find('iso-8859')!=-1:
 print 'gbk'+ft+">"+af
 commands.getoutput('iconv -ficonv -f gbk -t utf-8 -c -o'+""+af+""+af)

trans(os.getcwd())

py2.6以下版本可用代码

import os,sys 
 
def convert( filename, in_enc = "GBK", out_enc="UTF8" ): 
  try: 
    print "convert " + filename, 
    content = open(filename).read() 
    new_content = content.decode(in_enc).encode(out_enc) 
    open(filename, 'w').write(new_content) 
    print " done" 
  except: 
    print " error" 
 
def explore(dir): 
  for root, dirs, files in os.walk(dir): 
    for file in files: 
      path = os.path.join(root, file) 
      convert(path) 
 
def main(): 
  for path in sys.argv[1:]: 
    if os.path.isfile(path): 
      convert(path) 
    elif os.path.isdir(path): 
      explore(path) 
 
if __name__ == "__main__": 
  main()

支持py3.1的版本

import os
import sys
import codecs
#该程序用于将目录下的文件从指定格式转换到指定格式,默认的是GBK转到utf-8 
def convert(file,in_enc="GBK",out_enc="UTF-8"):
try:
print ("convert " +file)
f=codecs.open(file,'r',in_enc)
new_content=f.read()
codecs.open(file,'w',out_enc).write(new_content)
#print (f.read())
except IOError as err:
print ("I/O error: {0}".format(err))


def explore(dir):
for root,dirs,files in os.walk(dir):
for file in files:
path=os.path.join(root,file)
convert(path)

def main():
for path in sys.argv[1:]:
if(os.path.isfile(path)):
convert(path)
elif os.path.isdir(path):
explore(path)

if __name__=="__main__":
main()

以上所述就是本文 的全部内容了,希望大家能够喜欢。

Python 相关文章推荐
python连接MySQL数据库实例分析
May 12 Python
python并发编程之多进程、多线程、异步和协程详解
Oct 28 Python
Python制作词云的方法
Jan 03 Python
Python内建模块struct实例详解
Feb 02 Python
python之DataFrame实现excel合并单元格
Feb 22 Python
matplotlib subplots 调整子图间矩的实例
May 25 Python
selenium+python 对输入框的输入处理方法
Oct 11 Python
Python线性拟合实现函数与用法示例
Dec 13 Python
Python中单线程、多线程和多进程的效率对比实验实例
May 14 Python
解决tensorflow由于未初始化变量而导致的错误问题
Jan 06 Python
python开发实例之Python的Twisted框架中Deferred对象的详细用法与实例
Mar 19 Python
Django项目在pycharm新建的步骤方法
Mar 02 Python
Python实现批量下载文件
May 17 #Python
python抓取最新博客内容并生成Rss
May 17 #Python
Python实现遍历数据库并获取key的值
May 17 #Python
Python对列表排序的方法实例分析
May 16 #Python
python中base64加密解密方法实例分析
May 16 #Python
python中threading超线程用法实例分析
May 16 #Python
python实现合并两个数组的方法
May 16 #Python
You might like
PHP查询快递信息的方法
2015/03/07 PHP
PHP性能分析工具xhprof的安装使用与注意事项
2017/12/19 PHP
用javascript实现的支持lrc歌词的播放器
2007/05/17 Javascript
基于jquery的禁用右键、文本选择功能、复制按键的实现代码
2013/08/27 Javascript
Jquery模仿Baidu、Google搜索时自动补充搜索结果提示
2013/12/26 Javascript
JavaScript中的toUTCString()方法使用详解
2015/06/12 Javascript
IntelliJ IDEA 安装vue开发插件的方法
2017/11/21 Javascript
vue项目搭建以及全家桶的使用详细教程(小结)
2018/12/19 Javascript
详解Vue中watch对象内属性的方法
2019/02/01 Javascript
javascript将16进制的字符串转换为10进制整数hex
2020/03/05 Javascript
jquery实现鼠标悬浮弹出气泡提示框
2020/12/23 jQuery
[04:55]完美世界副总裁蔡玮:DOTA2的自由、公平与信任
2013/12/18 DOTA
[56:46]2018DOTA2亚洲邀请赛 3.31 小组赛 B组 VP vs Effect
2018/04/01 DOTA
[20:30]职业巡回赛回顾
2018/08/09 DOTA
Python编写屏幕截图程序方法
2015/02/18 Python
python如何实现不可变字典inmutabledict
2020/01/08 Python
Pycharm连接远程服务器过程图解
2020/04/30 Python
Python+PyQt5+MySQL实现天气管理系统
2020/06/16 Python
vscode调试django项目的方法
2020/08/06 Python
详解Python中@staticmethod和@classmethod区别及使用示例代码
2020/12/14 Python
浅析python字符串前加r、f、u、l 的区别
2021/01/24 Python
澳大利亚在线批发商:Simply Wholesale
2021/02/24 全球购物
几个Shell Script面试题
2014/04/18 面试题
实习教师自我鉴定
2013/09/27 职场文书
啤酒节策划方案
2014/05/28 职场文书
集体生日活动方案
2014/08/18 职场文书
励志演讲稿300字
2014/08/21 职场文书
医德医魂心得体会
2014/09/11 职场文书
盗窃案辩护词
2015/05/21 职场文书
老兵退伍感言
2015/08/03 职场文书
学法用法心得体会(2016推荐篇)
2016/01/21 职场文书
Nginx服务器添加Systemd自定义服务过程解析
2021/03/31 Servers
python如何读取.mtx文件
2021/04/22 Python
Python selenium的这三种等待方式一定要会!
2021/06/10 Python
教你用Python matplotlib库制作简单的动画
2021/06/11 Python
教你如何让spark sql写mysql的时候支持update操作
2022/02/15 MySQL