概述
#!/usr/bin/env python
# encoding: utf-8
"""
@author: wanwei
@license: (C) Copyright 2013-2017, Node Supply Chain Manager Corporation Limited.
@contact: wei_wan
@software: pycharm
@file: batch_convert_encoding.py
@time: 2019/5/28 14:38
@desc:
"""
import os
import sys
in_enc="gbk"
out_enc="utf-8"
def convert(filename):
in_enc = globals()['in_enc']
out_enc = globals()['out_enc']
try:
with open(filename, 'r', encoding=in_enc) as f:
content = f.read()
with open(filename, 'w', encoding=out_enc) as f1:
f1.write(content)
print(filename + "................done")
except IOError as err:
print("{0}
ERROR:{1}".format(err.args[0], err.args[1]))
except UnicodeDecodeError as err2:
print(err2.with_traceback())
def explorer(dir):
for root, dirs, files in os.walk(dir):
for file in files:
path = os.path.join(root, file)
if os.path.isfile(path):
convert(path)
if os.path.isdir(path):
explorer(path)
def judge_file_dir(path):
if os.path.isfile(path):
convert(path)
if os.path.isdir(path):
explorer(path)
def main():
for path in sys.argv[1:]:
path = os.path.join(path)
judge_file_dir(path)
if __name__ == "__main__":
sys.argv.append(r'E:技术资料python数据分析')
main()
最后
以上就是潇洒灯泡为你收集整理的Python批量转换文件编码格式的全部内容,希望文章能够帮你解决Python批量转换文件编码格式所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复