I created a helper function that does the job:
import os def get_path_tail(path, tail_length = 1): assert isinstance(tail_length, int) if tail_length == 1: return path.split('/')[-tail_length] elif tail_length > 1: return os.path.join(*path.split('/')[-tail_length:])
Behavior:
>>> path = os.path.join('C:','foo','bar', 'baz','text.txt') >>> print get_path_tail(path, tail_length = 3) bar/baz/text.txt
His signature is in ForeverWintr’s answer, but I couldn’t comment on his answer, because I don’t have enough reputation yet. :)
source share