Open file with Unicode file name?

It seems I can not open the file with the file name in Unicode. Lets say:

for i in os.listdir(): open(i, 'r') 

When I try to find some solution, I always get pages on how to read and write a string in unicode to a file, and not how to open a file with file() or open() with a Unicode name.

+8
python file unicode
source share
2 answers

Just pass open() unicode string for the file name:

In Python 2.x:

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

In Python 3.x, all lines are Unicode, so there is nothing in it.

As always, note that the best way to open a file always uses the with statement in combination with open() .

Edit: Regarding os.listdir() , the tip is changing again, in Python 2.x you have to be careful:

os.listdir (), which returns file names, causes a problem: should it return a Unicode version of file names or should it return 8-bit strings containing encoded versions? os.listdir () will do both, depending on whether the directory path was specified as an 8-bit string or a Unicode string. If you pass a Unicode string as a path, the file names will be decoded using the file system encoding, a list of Unicode strings will be returned, and transmitting an 8-bit path will return 8-bit versions of the file names.

A source

In short, if you want Unicode to come out, put Unicode in:

 >>> os.listdir(".") ['someUnicodeFilename\xce\xbb', 'old', 'Dropbox', 'gdrb'] >>> os.listdir(u".") [u'someUnicodeFilename\u03bb', u'old', u'Dropbox', u'gdrb'] 

Please note that the file will still be open anyway - it will not display well inside Python, since it will be an 8-bit string, but it will still work.

 open('someUnicodeFilename\xce\xbb') <open file 'someUnicodeFilenameλ', mode 'r' at 0x7f1b97e70660> 

Under 3.x, as always, it is always Unicode.

+27
source share

You can try the following:

 import os import sys for filename in os.listdir(u"/your-direcory-path/"): open(filename.encode(sys.getfilesystemencoding()), "r") 
+7
source share

All Articles