我正在编写一个脚本,必须移动一些文件,但不幸的是,似乎os.path
没有很好的国际化。当我有希伯来语命名的文件时,有问题。这是目录内容的屏幕截图:
(来源:thegreenplace.net)
现在考虑这个代码遍历此目录中的文件:
files = os.listdir('test_source')
for f in files:
pf = os.path.join('test_source', f)
print pf, os.path.exists(pf)
输出是:
test_source\ex True
test_source\joe True
test_source\mie.txt True
test_source\__()'''.txt True
test_source\????.txt False
请注意os.path.exists
如何认为希伯来语命名的文件甚至不存在?我怎样才能解决这个问题?
Windows XP Home SP2上的ActivePython 2.5.2
嗯,在some digging之后,似乎在为os.listdir提供unicode字符串时,这种方式有效:
files = os.listdir(u'test_source')
for f in files:
pf = os.path.join(u'test_source', f)
print pf.encode('ascii', 'replace'), os.path.exists(pf)
===>
test_source\ex True
test_source\joe True
test_source\mie.txt True
test_source\__()'''.txt True
test_source\????.txt True
这里的一些重要观察:
os.listdir
(和类似的函数,如os.walk
)应该传递一个unicode字符串,以便与unicode路径一起正常工作。以下是上述链接的引用:os.listdir(),它返回文件名,引发了一个问题:它应该返回Unicode版本的文件名,还是应该返回包含编码版本的8位字符串? os.listdir()将执行这两个操作,具体取决于您是将目录路径提供为8位字符串还是Unicode字符串。如果传递Unicode字符串作为路径,则将使用文件系统的编码对文件名进行解码,并返回Unicode字符串列表,而传递8位路径将返回文件名的8位版本。
print
想要一个ascii字符串,而不是unicode,所以路径必须编码为ascii。它看起来像Unicode和ASCII问题 - os.listdir
返回一个ASCII字符串列表。
编辑:我在Python 3.0上尝试过,也在XP SP2上,os.listdir
只是省略了希伯来文件名而不是列出它们。
根据文档,这意味着它无法解码它:
请注意,当os.listdir()返回字符串列表时,将省略无法正确解码的文件名,而不是引发UnicodeError。
它在OS X上使用Python 2.5.1就像一个魅力:
subdir/bar.txt True
subdir/foo.txt True
subdir/עִבְרִית.txt True
也许这意味着这与Windows XP有什么关系呢?
编辑:我也尝试使用unicode字符串来尝试更好地模仿Windows行为:
for f in os.listdir(u'subdir'):
pf = os.path.join(u'subdir', f)
print pf, os.path.exists(pf)
subdir/bar.txt True
subdir/foo.txt True
subdir/עִבְרִית.txt True
在终端(os x stock命令提示应用程序)中。使用IDLE它仍然有效,但没有正确打印文件名。为了确保它确实是unicode,我检查了:
>>>os.listdir(u'listdir')[2]
u'\u05e2\u05b4\u05d1\u05b0\u05e8\u05b4\u05d9\u05ea.txt'
问号是当unicode字符无法以特定编码表示时显示的或多或少的通用符号。 Windows下的终端或交互式会话可能使用ASCII或ISO-8859-1等。所以实际的字符串是unicode,但它被转换为????当打印到终端时。这就是为什么它适用于PEZ,使用OSX。