import os >>> base=os.path.basename('/root/dir/sub/file.ext') >>> base 'file.ext' >>> os.path.splitext(base) ('file', '.ext') >>> os.path.splitext(base)[0] 'file'

Read more of this post