python - os.listdir 正在删除字符重音

标签 python windows python-2.7 unicode python-2.6

在 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]

os.listdir(path) :

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/

相关文章:

python - 在 alpine docker 容器中安装 psycopg2

python - 连续时间输入函数的低通滤波器(在python中)的实现

python - 使用多处理时克服内存限制

windows - 无法使用 apm 在 Windows 7 64 位机器上安装 atom-beautify

python - 如果元数据不存在,PDF-Plumber 提取标题

python - 用类创建对象,为什么需要__init__(self, args) :?

Java 奇怪的行为。 .jar 无法打开

windows - ForFiles 命令在 Windows 7 和 Windows 2008 Server 上返回不同的结果

python - 如何选择在python中写入(.csv)的列

python-3.x - opencv 中的 bin/example_reg_map_test 错误