Python批量转换文件编码格式
作者:hebedich 发布时间:2021-10-07 05:01:22
标签:Python,批量转换,文件编码格式
自己写的方法,适用于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()
以上所述就是本文 的全部内容了,希望大家能够喜欢。
0
投稿
猜你喜欢
- google找到这样一个解决方法: 在上传控件中插入了值,就只能通过form的reset功能来清空了,但是form里面其他的值也被reset
- 按下"开始(win)"按钮和R键,输入cmd,打开命令行寻找点击需要的库:https://www.lfd.uci.edu
- 方法一 <%dim total(7,3) total(1,0)="ASP之家"&n
- PyQt5不规则窗口实现动画效果实例import sysfrom PyQt5.QtCore import *from PyQt5.QtGui
- 1、jsp前端<%-- Created by IntelliJ IDEA. User: Lenovo Date: 2020/6/19
- BeautifulSoup简介Beautiful Soup是python的一个库,最主要的功能是从网页抓取数据。官方解释如下:Beautif
- format是字符串内嵌的一个方法,用于格式化字符串。以大括号{}来标明被替换的字符串。1、基本用法1. 按照{}的顺序依次匹配括号中的值s
- WSGI协议首先弄清下面几个概念:WSGI:全称是Web Server Gateway Interface,WSGI不是服务器,python
- 最近要搭建一个阿里云的LMAP环境,选了CentOS7来做搭建。1.ApacheCentos7默认已经安装httpd服务,只是没有启动。如果
- 很高兴参加了这一期的薯片会,认识了几个朋友~~不料的却是今天我要来总结一下本次薯片会我们总共讨论了三个议题:A、 如何让“用户”更容易识别超
- 平时我们写个脚本,要放到后台执行去,我们怎么做呢?nohup python example.py 2>&1 /dev/null
- Hi, 大家好~ 好久没有发有营养的东西,今天就扔一篇最近热点的Google Chrome 浏览器的试用心得吧。先说个比较搞的事情,Goog
- 如果你过分信任 Access 2000数据库的密码保护,你可能会因此而蒙受损失。这是因为Access 2000的数据库级密码并不安全,相反它
- 本文实例讲述了python简单猜数游戏。分享给大家供大家参考。具体实现方法如下:#!/usr/bin/env pythonimport ra
- 判断访问是否来自搜索引擎的函数,有兴趣的可以试试! <% '检查当前用户是否是蜘蛛人 Function check(
- 我们在使用Django过程中,数据库往往是离不开的,比较长常用的是MySQL数据库,但在使用过程中,对Python不同的版本对用的库也不一样
- SEO是指搜索引擎优化,主要就是通过对网站的结构、标签、排版等各方面的优化,使搜索引擎更容易抓取网站的内容,并且让网站的各个网页在等搜索引擎
- return 语句用于退出函数,向调用方返回一个表达式。执行到 return 语句时,会退出函数,return 之后的语句不再执行。如:de
- 此站:http://www.cbmland.com/ 的页面离开时的效果非常NB!佩服的很。一开始,我以为是用事件 onunload,试了一
- 如何做一个只搜索本网站的引擎? 用下面两个文件即可实现:searchfiles.html &l