Convert python filenames to unicode

后端 未结 6 1163
星月不相逢
星月不相逢 2020-12-16 13:13

I am on python 2.6 for Windows.

I use os.walk to read a file tree. Files may have non-7-bit characters (German \"ae\" for example) in their filenames. These are enco

6条回答
  •  甜味超标
    2020-12-16 14:03

    os.walk isn't specified to always use os.listdir, but neither is it listed how Unicode is handled. However, os.listdir does say:

    Changed in version 2.3: On Windows NT/2k/XP and Unix, if path is a Unicode object, the result will be a list of Unicode objects. Undecodable filenames will still be returned as string objects.

    Does simply using a Unicode argument work for you?

    for dirpath, dirnames, filenames in os.walk(u"."):
      print dirpath
      for fn in filenames:
        print "   ", fn
    

提交回复
热议问题