文件名的Python问题

标签 python unicode directory

我想找到一个函数,它使用 os.listdir() 函数(或任何其他方法)访问目录并返回该目录中的所有文件名,但将非 ASCII 字符转换为它们的 unicode 格式。例如,如果我有文件 Hello WorlЪ.py,我希望函数返回 Hello Worl\u042a.py 或类似的东西。感谢您的帮助。

最佳答案

如果您向 os.listdir 传递一个 unicode 路径,则 os.listdir 返回 unicode:

os.listdir(u'.')

来自 the docs :

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.

关于文件名的Python问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16138305/

相关文章:

python - 当我想要列表时,后代与工厂男孩给出了 ManyRelatedManager

c++ - 如何在 Windows 下使用 mingw 使用 ifstream 打开 unicode 文件?

javascript - 如何仅遍历我实际看到的字符串中的字符?

C编程getcwd变量操作

java - 使用android从web目录读取文件

windows - 在大型目录结构上运行时出现元数据导出错误

python - 在 SeriesGroupBy 上执行 apply() 后如何将我的 Series 设为 "re-group"?

python - 使用 Dash 和 Plotly 实时更新表值

c - 如何检测 utf8 中使用的字母表?

python - 如何在 python 中将某些行存储在变量中?