Python2获取中文文件名的编码问题

来源:互联网 发布:服务器返回的数据错误 编辑:程序博客网 时间:2024/05/22 15:28

问题:

Python2获取包含中文的文件名是如果不转码会出现乱码。


这里假设要测试的文件夹名为test,文件夹下有5个文件名包含中文的文件分别为:

Python性能分析与优化.pdf
Python数据分析与挖掘实战.pdf
Python编程实战:运用设计模式、并发和程序库创建高质量程序.pdf
流畅的Python.pdf
编写高质量Python代码的59个有效方法.pdf


我们先不转码直接打印获取到的文件名,代码如下:

import osfor file in os.listdir('./test'):    print(file)
输出乱码:

Python���ܷ������Ż.pdfPython���ݷ������ھʵս.pdfPython���ʵս���������ģʽ�������ͳ���������������.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 osimport chardetfor file in os.listdir('./test'):    r = file.decode('GB2312')    print(r)

输出:
Python性能分析与优化.pdfPython数据分析与挖掘实战.pdfPython编程实战:运用设计模式、并发和程序库创建高质量程序.pdf流畅的Python.pdf编写高质量Python代码的59个有效方法.pdf
经过编码之后,文件名打印正确。

PS:chardet.detect检测的字符串越长越准确,越短越不准确

这里还有一个问题是上面的代码是在Windows下测试,Linux下文件名编码是utf-8,为了兼容Windows和Linux,代码需要修改一下,下面我们把代码封装到函数中:
# -*- coding: utf-8 -*-import osdef 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测试通过)。


原创粉丝点击