Python2 to get the encoding problem of Chinese filename

Source: Internet
Author: User
question:

Python2 get the file name that contains Chinese if the code will appear garbled.


This assumes that the folder you want to test is named Test, and there are 5 files in the folder that contain the Chinese file names:

Python performance analysis and optimization. pdf
Python data analysis and mining combat. pdf
Python programming Combat: Creating high-quality programs using design patterns, concurrency, and libraries. pdf
Fluent Python.pdf.
59 Effective ways to write high-quality Python code. pdf


We're going to print the name of the file we obtained directly, the code is as follows:


osos.listdir ('./test '):
    print(file)
Output garbled:

Pythonܷż.pdf
pythonݷھʵս.pdf
pythonʵսģʽͳⴴ.pdf
python.pdf
Дpython 59ч.pdf

Solution: First Test the file name encoding, here we use the Chardet module, install the command:

Pip Install Chardet

Use the Chardet.detect function to detect how the file name is encoded:
{' 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 '}
You can see that the encoding GB2312 the most confidence, the following we use GB2312 encoding to decode the filename, the code is as follows:
Os

chardetos.listdir ('./test '): R = File.decode (' GB2312 ')
     Print(R)

Output:
Python performance analysis and optimization. PDF python
data Analysis and mining. PDF
Python programming Combat: Create high quality programs using design patterns, concurrency, and libraries. pdf
Fluent python.pdf
59 Effective ways to write high-quality Python code. pdf
After encoding, the filename is printed correctly.
PS:chardet.detect detected strings longer and more accurate, shorter and less accurate
Here's another problem is that the above code is tested in Windows, Linux under the file name code is UTF-8, in order to compatible with Windows and Linux, the code needs to be modified, the following we encapsulate the code into the function:
#-*-Coding:utf-8-*-

os


get_filename_from_dir (dir_path):
    file_list = []
     os.path.exists (Dir_path):
        file_list

    os.listdir (dir_path):
        basename = Os.path.basename (item)
        # Print (Chardet.detect (basename))   # Find filename encoding, filename contains Chinese

        # The file encoding under Windows is gb2312,linux to Utf-8
        try:
            decode_str = Basename.decode ("GB2312")
         unicodedecodeerror:
            decode_str = Basename.decode ("Utf-8")

        file_list.append (DECODE_STR)

     file_list

# test code
r = Get_filename_from_dir ('./test ')
r:
    Print (i)


First with GB2312 decoding, if the error is then decoded with Utf-8, this will be compatible with Windows and Linux (in Win7 and Ubuntu16.04 test pass).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.