bytes.rsplit() returns a list of bytes objects, just like split(), but it makes its cuts starting from the right.
Without maxsplit the result is identical to split(). With maxsplit, the rightmost fields are separated and everything to the left stays in one piece, which is exactly what you want for extensions, last path components, and trailing values.
bytes.rsplit() is a method of the built-in bytes type: call it on a bytes object with dot notation, such as data.rsplit(...). No import is needed, and bytearray objects have the same method.
Syntax
Same arguments as split().
python
parts = data.rsplit()
parts = data.rsplit(sep)
parts = data.rsplit(sep, maxsplit)
Call
What it does
b"a.b.c".rsplit(b".", 1)
Returns [b'a.b', b'c'].
b"a.b.c".split(b".", 1)
Returns [b'a', b'b.c'] for comparison.
b"a b c".rsplit(None, 1)
Returns [b'a b', b'c'].
The list is still in left-to-right order; only the choice of which separators to cut changes.
Method signatures
python
bytes.rsplit(sep=None, maxsplit=-1)
# sep: bytes-like separator, or None for runs of ASCII whitespace
Parameters
Parameter
Description
sep
Optional. The separator bytes. None (default) splits on runs of whitespace.
maxsplit
Optional. Maximum number of splits, counted from the right; -1 means no limit.
Return Value
Return Value
Description
list
A list of bytes objects in their original order.
Python Examples
Example 1 — Split a Path at the Last Dot and Slash
A log path has dots in the file name. rsplit() with maxsplit=1 isolates just the final extension or file name.