python 读取文件夹,目录中出现中文的问题

在使用python读取中文目录的名称的时候,会出现中文乱码的问题,该问题很严重,因为使用os.path.isdir('乱码名称')和ospath.isfile('乱码名称')是判断不正确,都为false。

即使有时候采用 filename.decode("gbk").encode("utf-8") 问题会依然存在

# -*- coding: utf-8 -*-
import os


def listfiels(path):
    path = path.replace("\\", "/")
    mlist = os.listdir(path)

    for m in mlist:
        mpath = os.path.join(path, m)
        if os.path.isfile(mpath):
            pt = os.path.abspath(mpath)
            # print pt.decode("gbk").encode("utf-8") #会报错
            print pt
        else:
            pt = os.path.abspath(mpath)
            print pt
            listfiels(pt)


listfiels(unicode("D:/test_temp/tmp_autonav/17Q2_A5_20170630/ROOT/ALL/WIDE_BACKGROUND","utf-8"))
    

  重点在于使用 

unicode(path,"utf-8")

 

posted @ 2017-10-09 19:18  百变小超  阅读(29868)  评论(1编辑  收藏  举报