Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
301 views
in Technique[技术] by (71.8m points)

python - open file with a unicode filename?

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')

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Simply pass open() a unicode string for the file name:

In Python 2.x:

>>> open(u'someUnicodeFilenameλ')
<open file u'someUnicodeFilenameu03bb', mode 'r' at 0x7f1b97e70780>

In Python 3.x, all strings are Unicode, so there is literally nothing to it.

As always, note that the best way to open a file is always using the with statement in conjunction with open().

Edit: With regards to os.listdir() the advice again varies, under Python 2.x, you have to be careful:

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.

Source

So in short, if you want Unicode out, put Unicode in:

>>> os.listdir(".")
['someUnicodeFilenamexcexbb', 'old', 'Dropbox', 'gdrb']
>>> os.listdir(u".")
[u'someUnicodeFilenameu03bb', u'old', u'Dropbox', u'gdrb']

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('someUnicodeFilenamexcexbb')
<open file 'someUnicodeFilenameλ', mode 'r' at 0x7f1b97e70660>

Under 3.x, as always, it's always Unicode.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...