python批量修改文件编码格式的方法
本文实例为大家分享了python批量修改文件编码格式的具体代码,供大家参考,具体内容如下
使用说明:
1、使用工具:Python2.7.6+chardet2.3.0,chardet2.3.0下载地址:点击这里
2、环境配置:Python安装+配置环境变量,chardet解压放在Python安装目录\Lib\site-packages下
举例:批量修改当前路径下所有.cpp文件的编码格式为UTF-8,代码如下:
python:
importos
importsys
importcodecs
importchardet
defconvert(filename,out_enc="UTF-8"):
try:
content=codecs.open(filename,'r').read()
source_encoding=chardet.detect(content)['encoding']
printsource_encoding
content=content.decode(source_encoding).encode(out_enc)
codecs.open(filename,'w').write(content)
exceptIOErroraserr:
print("I/Oerror:{0}".format(err))
defexplore(dir):
forroot,dirs,filesinos.walk(dir):
forfileinfiles:
ifos.path.splitext(file)[1]=='.cpp':
printfile
path=os.path.join(root,file)
convert(path)
defmain():
explore(os.getcwd())
if__name__=="__main__":
main()
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下