【问题标题】:Python: Read only file name, instead of path [duplicate]Python:只读文件名,而不是路径[重复]
【发布时间】:2017-11-28 02:24:13
【问题描述】:

如何仅获取文件名而不是完整路径?

例如:

path = /folder/file.txt

我需要得到:

filename = file.txt

怎么做?

【问题讨论】:

    标签: python path filenames


    【解决方案1】:

    您应该使用os 模块:

    import os
    filename = os.path.basename(path)
    

    对于其他路径操作,请查看 here(适用于 python 2.7)或 here(适用于 python 3)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-29
      • 2012-09-13
      • 2012-03-01
      • 1970-01-01
      • 2020-04-04
      • 1970-01-01
      • 2019-11-16
      • 2021-10-15
      相关资源
      最近更新 更多