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

前端 未结 23 1890
逝去的感伤
逝去的感伤 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:25

    If you want to keep the path to the file and just remove the extension

    >>> file = '/root/dir/sub.exten/file.data.1.2.dat'
    >>> print ('.').join(file.split('.')[:-1])
    /root/dir/sub.exten/file.data.1.2
    

提交回复
热议问题