Convert python filenames to unicode

后端 未结 6 1161
星月不相逢
星月不相逢 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:18

    No, they are not encoded in Pythons internal string representation, there is no such thing. They are encoded in the encoding of the operating system/file system. Passing in unicode works for os.walk though.

    I don't know how os.walk behaves when filenames can't be decoded, but I assume that you'll get a string back, like with os.listdir(). In that case you'll again have problems later. Also, not all of Python 2.x standard library will accept unicode parameters properly, so you may need to encode them as strings anyway. So, the problem may in fact be somewhere else, but you'll notice if that is the case. ;-)

    If you need more control of the decoding you can't always pass in a string, and then just decode it with filename = filename.decode() as usual.

提交回复
热议问题