首页 > 解决方案 > 从 Python 中的文件夹名称中删除 ./

问题描述

我正在运行以下代码:

foldarz = [ f.path for f in os.scandir('.') if f.is_dir() ]
print(foldarz)

这将正确打印文件夹名称,但前面带有“.\”。我不想要“。\”。

我知道我可以拆分字符串等,但我认为有更好的方法而不必调用另一个函数。

标签: pythonpython-3.xformat

解决方案


你可以这样做os.path.basename

foldarz = [os.path.basename(f) for f in os.scandir('.') if f.is_dir()] 

推荐阅读