开发者社区> 问答> 正文

文件路径名的操作

你需要使用路径名来获取文件名,目录名,绝对路径等等。

展开
收起
哦哦喔 2020-04-17 12:26:16 965 0
1 条回答
写回答
取消 提交回答
  • 使用 os.path 模块中的函数来操作路径名。 下面是一个交互式例子来演示一些关键的特性:

    import os path = '/Users/beazley/Data/data.csv'

    Get the last component of the path

    os.path.basename(path) 'data.csv'

    Get the directory name

    os.path.dirname(path) '/Users/beazley/Data'

    Join path components together

    os.path.join('tmp', 'data', os.path.basename(path)) 'tmp/data/data.csv'

    Expand the user's home directory

    path = '~/Data/data.csv' os.path.expanduser(path) '/Users/beazley/Data/data.csv'

    Split the file extension

    os.path.splitext(path) ('~/Data/data', '.csv')

    2020-04-17 12:26:24
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载