Path to a file without basename

前端 未结 4 1073
轮回少年
轮回少年 2021-02-03 16:24

How can I get the path of a file without the file basename ?

Something like /a/path/to/my/file.txt --> /a/path/to/my/

Tried with .split

相关标签:
4条回答
  • 2021-02-03 17:10

    You can import os

    >>> filepath
    '/a/path/to/my/file.txt'
    >>> os.path.dirname(filepath)
    '/a/path/to/my'
    >>> 
    
    0 讨论(0)
  • 2021-02-03 17:19

    Use os.path.dirname(filename).

    0 讨论(0)
  • 2021-02-03 17:31
    (dirname, filename) = os.path.split(path)
    
    0 讨论(0)
  • 2021-02-03 17:31

    Check subs of os.path

    os.path.dirname('/test/one')
    
    0 讨论(0)
提交回复
热议问题