maxsplit in os.path.split

The "str.split" and "str.rsplit" methods have a useful "maxsplit" option, which lets you set the number of times to split, defaulting to -1 (which is "unlimited"). The corresponding "os.path.split", however, has no "maxsplit" option. It can only split once, which splits the last path segment (the "basename") from the rest (equivalent of "str.rsplit" with "maxsplit=1"). 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".

On 28 September 2015 at 12:46, Todd <toddrjen@gmail.com> wrote:
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 https://pypi.python.org/pypi/pathlib/ Paul

On 28 September 2015 at 12:46, Todd <toddrjen@gmail.com> wrote:
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 https://pypi.python.org/pypi/pathlib/ Paul
participants (3)
-
Guido van Rossum
-
Paul Moore
-
Todd