python - 如何获取没有目录名的文件名

标签 python

如何在结果中只列出目录中没有目录信息的文件名?我试过了

for file in glob.glob(dir+filetype):
    print file

给我结果 /path_name/1.log,/path_name/2.log,.... 但我只需要文件名:1.log2.log 等。我不需要结果中的目录信息。有没有简单的方法来摆脱路径信息?我不想对结果进行一些修改。谢谢!

最佳答案

os.path.basename :

Return the base name of pathname path. This is the second element of the pair returned by passing path to the function split(). Note that the result of this function is different from the Unix basename program; where basename for '/foo/bar/' returns 'bar', the basename() function returns an empty string ('').

所以:

>>> os.path.basename('/path_name/1.log,/path_name/2.log')
'2.log'

关于python - 如何获取没有目录名的文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16090670/

相关文章:

python - 即使似乎已安装,也无法导入 scikits-learn

python - 一个关于python排序效率的问题

python - 在python中生成列表并将其存储在mysql数据库中的问题

python - 如何循环访问 Jupyter/iPython Notebook 中的多个单元格

python - Scikit-learn:我的线性回归不是直线,它很乱

python - 设置面积图 openpyxl 的透明度(alpha)

python - 如何仅在 Heroku https 上制作 python?

python - Django JsonField 到表单

python - 如何在 django 中添加模板 css js 和 bootstrap

python - Pydev,eclipse,当我按下 时暂停。在交互模式下