在 Windows 文件资源管理器中,创建一个新的 txt 文件并将其命名为 Ń.txt
(注意 N
上的重音符号)。
按住 shift 并右键单击您创建 Ń.txt
的文件夹,然后选择在此处打开命令窗口(或者打开 cmd.exe 并 cd 进入您创建文件的目录)。
在 python 终端中运行:
print os.listdir(".") #note that the file is displayed as "N.txt"
print map(os.path.exists,os.listdir(".")) #note the file doesn't exist???
我已经尝试了很多解码,但是 os.listdir
根本没有返回实际文件名的字节串,所以编码/解码不正确的字节仍然是不正确的字节。
最佳答案
在此之前使用u
:
>>> print os.listdir(u".")
[u'\u0143.txt']
>>> print map(os.path.exists,os.listdir(u"."))
[True]
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.
关于python - os.listdir 正在删除字符重音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21613650/