问题描述
我似乎无法打开具有Unicode文件名的文件.可以说我这样做:
I don't seem to be able to open a file which has a unicode filename. Lets say I do:
for i in os.listdir():
open(i, 'r')
当我尝试寻找解决方案时,总是会得到有关如何向文件读写unicode字符串的页面,而不是有关如何使用具有Unicode名称的file()
或open()
打开文件的页面. /p>
When I try to search for some solution, I always get pages about how to read and write a unicode string to a file, not how to open a file with file()
or open()
which has a unicode name.
推荐答案
只需为文件名传递open()
一个unicode字符串:
Simply pass open()
a unicode string for the file name:
在Python 2.x中:
In Python 2.x:
>>> open(u'someUnicodeFilenameλ')
<open file u'someUnicodeFilename\u03bb', mode 'r' at 0x7f1b97e70780>
在Python 3.x中,所有字符串都是Unicode,因此实际上没有任何内容.
In Python 3.x, all strings are Unicode, so there is literally nothing to it.
一如既往,请注意,打开文件的最佳方法始终是使用 with
陈述与open()
结合.
As always, note that the best way to open a file is always using the with
statement in conjunction with open()
.
关于os.listdir()
的建议也有所不同,在Python 2.x下,您必须小心:
With regards to os.listdir()
the advice again varies, under Python 2.x, you have to be careful:
因此,简而言之,如果要使用Unicode,则将Unicode放入:
So in short, if you want Unicode out, put Unicode in:
>>> os.listdir(".")
['someUnicodeFilename\xce\xbb', 'old', 'Dropbox', 'gdrb']
>>> os.listdir(u".")
[u'someUnicodeFilename\u03bb', u'old', u'Dropbox', u'gdrb']
请注意,该文件仍然会以任何一种方式打开-由于它将是8位字符串,因此无法在Python中很好地表示,但是仍然可以正常工作.
Note that the file will still open either way - it won't be represented well within Python as it'll be an 8-bit string, but it'll still work.
open('someUnicodeFilename\xce\xbb')
<open file 'someUnicodeFilenameλ', mode 'r' at 0x7f1b97e70660>
在3.x下,一如既往,它始终是Unicode.
Under 3.x, as always, it's always Unicode.
这篇关于打开文件以unicode文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!