dasheng-maritime

在使用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")

 

分类:

技术点:

相关文章:

  • 2021-06-28
  • 2021-09-24
  • 2021-09-09
  • 2022-02-04
  • 2022-02-06
  • 2022-02-19
  • 2021-07-22
  • 2021-12-22
猜你喜欢
  • 2021-10-11
  • 2021-06-20
  • 2021-09-25
  • 2022-03-05
  • 2021-08-08
相关资源
相似解决方案