[Python-ideas] maxsplit in os.path.split

Paul Moore p.f.moore at gmail.com
Mon Sep 28 13:58:41 CEST 2015

On 28 September 2015 at 12:46, Todd <toddrjen at gmail.com> wrote:
> I think it would be useful if "os.path.split" also had a "maxsplit" option.
> This would default to "1" (the current behavior"), but could be set to any
> value allowed by "str.split".  Using this option would follow the behavior
> of "str.rsplit" for that value of "maxsplit".

In Python 3.6+ (which is the only place a change like this is likely
to happen) you're probably better using pathlib. There, you can use
path.parts, which returns a tuple of the path elements, so you can do
things like

    >>> Path('C:\\what\\ever\\you\\like.txt').parts[-3:]
    ('ever', 'you', 'like.txt')

That's usable now in Python 3.4+, and a backport is available at


More information about the Python-ideas mailing list