import os
print(os.path.basename(your_path))
from pathlib import Path
>>> p = r"C:UsersshaliOneDrivecodesetTheory.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']
filename = os.path.basename("path/to/file/sample.txt")
print (os.path.basename("/path/to/file.txt"))
# file.txt
from pathlib import Path
Path("/tmp/d/a.dat").name