os.path.basename(filename)[-16:-3] code example Example: file base name and extension python from pathlib import Path Path('/root/dir/sub/file.ext').stem