打开文件以unicode文件名? [英] open file with a unicode filename?

查看:109
本文介绍了打开文件以unicode文件名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法打开具有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:

返回文件名的

os.listdir()引起了一个问题:它应该返回文件名的Unicode版本,还是应该返回包含编码版本的8位字符串? os.listdir()会同时执行这两种操作,具体取决于您是以8位字符串还是Unicode字符串提供目录路径.如果您将Unicode字符串作为路径传递,则文件名将使用文件系统的编码进行解码,并返回Unicode字符串列表,而传递8位路径将返回文件名的8位版本.

os.listdir(), which returns filenames, raises an issue: should it return the Unicode version of filenames, or should it return 8-bit strings containing the encoded versions? os.listdir() will do both, depending on whether you provided the directory path as an 8-bit string or a Unicode string. If you pass a Unicode string as the path, filenames will be decoded using the filesystem’s encoding and a list of Unicode strings will be returned, while passing an 8-bit path will return the 8-bit versions of the filenames.

来源

因此,简而言之,如果要使用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文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆