Answered by : itchy-ibis-9ggba72x5riu
import os
print(os.path.basename(your_path))
Source : https://stackoverflow.com/questions/8384737/extract-file-name-from-path-no-matter-what-the-os-path-format | Last Update : Fri, 06 Mar 20
Answered by : luchao-qi
import os
filename = os.path.basename(path)
name, ext = os.path.splitext(filename)
Source : | Last Update : Fri, 07 Apr 23
Answered by : dangerous-dolphin-qzrno6107nob
filename = os.path.basename("path/to/file/sample.txt")
Source : https://www.kite.com/python/answers/how-to-extract-a-filename-from-a-path-in-python | Last Update : Tue, 07 Sep 21
Answered by : adventurous-addax-z62mj3vc4dnv
from pathlib import Path
>>> p = r"C:\Users\shali\OneDrive\code\setTheory.pdf"
>>> Path(p).anchor
'C:\\'
>>> Path(p).parent
WindowsPath('C:/Users/shali/OneDrive/code')
>>> Path(p).name
'setTheory.pdf'
>>> Path(p).stem
'setTheory'
>>> Path(p).suffixes
['.pdf']
Source : | Last Update : Fri, 11 Nov 22
Answered by : nguyen-quang-huy
print (os.path.basename("/path/to/file.txt"))
# file.txt
Source : | Last Update : Fri, 03 Jun 22
Answered by : said-faraj
from pathlib import Path
Path("/tmp/d/a.dat").name
Source : | Last Update : Tue, 26 Jul 22
Answered by : cruel-crane-0y4rxr2odhi3
from pathlib import Path
Path('/root/dir/sub/file.ext').stem
Source : https://stackoverflow.com/questions/678236/how-to-get-the-filename-without-the-extension-from-a-path-in-python?rq=1 | Last Update : Thu, 16 Dec 21