浅谈Python2获取中文文件名的编码问题
作者:xiemanR 时间:2021-08-11 08:18:45
问题:
Python2获取包含中文的文件名是如果不转码会出现乱码。
这里假设要测试的文件夹名为test,文件夹下有5个文件名包含中文的文件分别为:
Python性能分析与优化.pdf
Python数据分析与挖掘实战.pdf
Python编程实战:运用设计模式、并发和程序库创建高质量程序.pdf
流畅的Python.pdf
编写高质量Python代码的59个有效方法.pdf
我们先不转码直接打印获取到的文件名,代码如下:
import os
for file in os.listdir('./test'):
print(file)
输出乱码:
Python���ܷ������Ż�.pdf
Python���ݷ������ھ�ʵս.pdf
Python���ʵս���������ģʽ�������ͳ���ⴴ������������.pdf
������Python.pdf
�������Python�����59��������.pdf
解决:
先测试一下文件名的编码,这里我们用到chardet模块,安装命令:
pip install chardet
用chardet.detect函数检测一下文件名的编码方式:
{'confidence': 0.99, 'encoding': 'GB2312'}
{'confidence': 0.99, 'encoding': 'GB2312'}
{'confidence': 0.99, 'encoding': 'GB2312'}
{'confidence': 0.73, 'encoding': 'windows-1252'}
{'confidence': 0.99, 'encoding': 'GB2312'}
可以看出编码GB2312的置信度最大,下面我们用GB2312编码来解码文件名,代码如下:
import os
import chardet
for file in os.listdir('./test'):
r = file.decode('GB2312')
print(r)
输出:
Python性能分析与优化.pdf
Python数据分析与挖掘实战.pdf
Python编程实战:运用设计模式、并发和程序库创建高质量程序.pdf
流畅的Python.pdf
编写高质量Python代码的59个有效方法.pdf
经过编码之后,文件名打印正确。
PS:chardet.detect检测的字符串越长越准确,越短越不准确
这里还有一个问题是上面的代码是在Windows下测试,Linux下文件名编码是utf-8,为了兼容Windows和Linux,代码需要修改一下,下面我们把代码封装到函数中:
# -*- coding: utf-8 -*-
import os
def get_filename_from_dir(dir_path):
file_list = []
if not os.path.exists(dir_path):
return file_list
for item in os.listdir(dir_path):
basename = os.path.basename(item)
# print(chardet.detect(basename)) # 找出文件名编码,文件名包含有中文
# windows下文件编码为GB2312,linux下为utf-8
try:
decode_str = basename.decode("GB2312")
except UnicodeDecodeError:
decode_str = basename.decode("utf-8")
file_list.append(decode_str)
return file_list
# 测试代码
r = get_filename_from_dir('./test')
for i in r:
print(i)
先用GB2312解码,如果出错再用utf-8解码,这样就兼容了Windows和Linux(在Win7和Ubuntu16.04测试通过)。
来源:http://blog.csdn.net/xiemanR/article/details/72793860
标签:Python2,中文,文件名,编码
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
css布局查看器
2008-10-29 11:22:00
button标签的type属性默认值是?
2010-08-16 12:33:00
Python求解任意闭区间的所有素数
2023-10-12 00:07:12
分享整理的12条sql语句连同数据
2012-07-11 16:14:59
Internet Explorer 8 Beta2 功能预览
2008-07-29 13:20:00
![](https://img.aspxhome.com/file/UploadPic/20087/29/2008729132347173s.jpg)
如何在MySQL查询结果集中得到记录行号
2008-12-17 15:00:00
基于微服务框架go-micro开发gRPC应用程序
2023-06-22 21:19:27
![](https://img.aspxhome.com/file/2023/4/89014_0s.jpg)
Centos6.x服务器配置jdk+tomcat+mysql环境(jsp+mysql)
2023-06-14 12:14:13
![](https://img.aspxhome.com/file/2023/1/67221_0s.png)
纯ASP结合VML生成完美图-折线图
2010-05-11 16:50:00
jQuery.sheet – 创建Excel界面风格的jQuery在线应用
2010-01-27 13:03:00
![](https://img.aspxhome.com/file/UploadPic/20101/27/20101271369489s.jpg)
Css 清除浮动
2008-09-15 18:47:00
表单制作方式大比拼
2008-10-09 11:32:00
![](https://img.aspxhome.com/file/uploadpic/20071/2007131102013522.gif)
RichTextBox 显示图片和word的代码
2023-06-28 18:27:12
Web表单设计:表单结构
2011-04-22 12:32:00
JS实现拖动模糊框特效
2023-08-06 05:18:51
![](https://img.aspxhome.com/file/2023/0/55910_0s.gif)
Microsoft SQL Server数据库SA权限总结
2009-01-06 11:32:00
python函数参数(必须参数、可变参数、关键字参数)
2023-09-17 07:54:28
Python简单获取网卡名称及其IP地址的方法【基于psutil模块】
2022-10-07 19:52:15
MySQL数据库中与 ALTER TABLE 有关的问题
2009-01-14 11:57:00
如何隐藏IP地址的最后一位
2011-04-04 16:38:00