How to get the filename without the extension from a path in Python?

前端 未结 23 1866
逝去的感伤
逝去的感伤 2020-11-22 05:43

How to get the filename without the extension from a path in Python?

For instance, if I had "/path/to/some/file.txt", I would want "

23条回答
  •  温柔的废话
    2020-11-22 06:24

    https://docs.python.org/3/library/os.path.html

    In python 3 pathlib "The pathlib module offers high-level path objects." so,

    >>> from pathlib import Path
    >>> p = Path("/a/b/c.txt")
    >>> print(p.with_suffix(''))
    \a\b\c
    >>> print(p.stem)
    c
    

提交回复
热议问题