1 import os
2>>> base=os.path.basename('/root/dir/sub/file.ext')
3>>> base
4'file.ext'
5>>> os.path.splitext(base)
6('file', '.ext')
7>>> os.path.splitext(base)[0]
8'file'
1>>> import os
2>>> base=os.path.basename('/root/dir/sub/file.ext')
3>>> base
4'file.ext'
5>>> os.path.splitext(base)
6('file', '.ext')
7>>> os.path.splitext(base)[0]
8'file'
9
1#Using pathlib in Python 3.4+
2from pathlib import Path
3Path('/root/dir/sub/file.ext').stem