opening a file with an accented character in its name, in Python 2 on Windows

拟墨画扇 提交于 2019-12-24 19:24:32

问题


In a directory in Windows I have 2 files, both of them with an accented character in its name: t1û.fn and t2ű.fn; The dir command in the Command Prompt shows both correctly:

S:\p>dir t*.fn
 Volume in drive S is q
 Volume Serial Number is 05A0-8823

 Directory of S:\p

2017-09-03  14:54                 4 t1û.fn
2017-09-03  14:54                 4 t2ű.fn
               2 File(s)              8 bytes
               0 Dir(s)  19,110,621,184 bytes free

Screenshot:

However, Python can't see both files:

S:\p>python -c "import os; print [(fn, os.path.isfile(fn)) for fn in os.listdir('.') if fn.endswith('.fn')]"
[('t1\xfb.fn', True), ('t2u.fn', False)]

It looks like Python 2 uses a single-byte API for filenames, thus the accented character in t1û.fn is mapped to the single byte \xfb, and the accented character in t2ű.fn is mapped to the unaccented ASCII single byte u.

How is it possible to use a multi-byte API for filenames on Windows in Python 2? I want to open both files in the console version of Python 2 on Windows.


回答1:


Use a unicode string:

f1 = open(u"t1\u00fb.fn")     # t1û.fn
f2 = open(u"t2\u0171.fn")     # t2ű.fn


来源:https://stackoverflow.com/questions/46025407/opening-a-file-with-an-accented-character-in-its-name-in-python-2-on-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!