문제

In windows file explorer, create a new txt file and name it Ń.txt (note the accent over the N).

Hold shift and right click the folder where you created Ń.txt and select open command window here (or alternatively open cmd.exe and cd into the directory where you created the file).

Run in python terminal:

print os.listdir(".")  #note that the file is displayed as "N.txt"
print map(os.path.exists,os.listdir(".")) #note the file doesn't exist???

I have tried many decodings but os.listdir is not returning the bytestring of the actual filename at all, so encoding/decoding the incorrect bytes is still the incorrect bytes.

도움이 되었습니까?

해결책

Use u before that:

>>> print os.listdir(u".")
[u'\u0143.txt']
>>> print map(os.path.exists,os.listdir(u"."))
[True]

os.listdir(path):

Changed in version 2.3: On Windows NT/2k/XP and Unix, if path is a Unicode object, the result will be a list of Unicode objects. Undecodable filenames will still be returned as string objects.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top