我是靠谱客的博主 潇洒灯泡,这篇文章主要介绍Python批量转换文件编码格式,现在分享给大家,希望可以做个参考。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/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批量转换文件编码格式内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(51)

评论列表共有 0 条评论

立即
投稿
返回
顶部