gad*_*get 4 python directory unicode
我想找到一个使用os.listdir()函数(或任何其他方法)访问目录的函数,并返回该目录中的所有文件名,但将非ASCII字符转换为其unicode格式.例如,如果我有文件Hello Worl?.py,我希望函数返回Hello Worl\u042a.py或等效.任何帮助表示赞赏.
如果传递os.listdirunicode路径,则os.listdir返回unicode:
os.listdir(u'.')
Run Code Online (Sandbox Code Playgroud)
来自文档:
在版本2.3中更改:在Windows NT/2k/XP和Unix上,如果path是Unicode对象,则结果将是Unicode对象的列表.不可解码的文件名仍将作为字符串对象返回.