Home Python Bytes Methods splitlines() Python bytes.splitlines() Method Definition bytes.splitlines() returns a list of lines. For bytes, the only line boundaries are \n, \r\n, and \r, so files with Unix, Windows, or old Mac line endings are all handled.
By default the line endings are removed; pass keepends=True to keep them. A final line ending does not create an extra empty line, which makes it a better fit for file content than split(b"\n").
bytes.splitlines() is a method of the built-in bytes type : call it on a bytes object with dot notation, such as data.splitlines(...). No import is needed, and bytearray objects have the same method.
Syntax Optionally keep the line-ending bytes.
lines = data.splitlines()
lines = data.splitlines(keepends=True) Call What it does b"a\nb\r\nc".splitlines()Returns [b'a', b'b', b'c']. b"a\n".splitlines(True)Returns [b'a\n']. b"".splitlines()Returns [].
str.splitlines() also splits on characters such as \x0b, \x0c, and \x1c; the bytes version does not.
Method signatures bytes.splitlines(keepends=False)
# line boundaries for bytes: \n, \r\n, \r Parameters Parameter Description keepends Optional. If true, each line keeps its line-ending bytes. Default False.
Return Value Return Value Description list A list of bytes objects, one per line.
Python Examples 1. Mixed line endings 2. Bytes boundaries only 3. Trailing newline Example 1 — Split Data with Mixed Line Endings One buffer contains LF, CRLF, and CR endings. splitlines() handles all three, with or without keeping them.
data = b"one\ntwo\r\nthree\rfour"
print(data.splitlines())
print(data.splitlines(keepends=True)) [b'one', b'two', b'three', b'four']
[b'one\n', b'two\r\n', b'three\r', b'four'] How It Works Three styles. \n, \r\n, and \r each end a line.CRLF is one boundary. The two bytes \r\n produce one break, not an empty line between them.Keep the endings. With keepends=True each line keeps exactly the bytes that ended it; the last line had none.Example 2 — Bytes Ignore Other Line Breaks Vertical tab and form feed are line breaks for str, but not for bytes. Compare both on the same data.
raw = b"a\x0bb\x0cc\nd"
print(raw.splitlines())
print(raw.decode("ascii").splitlines()) [b'a\x0bb\x0cc', b'd']
['a', 'b', 'c', 'd'] How It Works Only LF splits. In bytes mode, \x0b and \x0c are ordinary bytes inside the first line.str is broader. After decoding, str.splitlines() treats vertical tab and form feed as line breaks too.Choose deliberately. Use bytes for protocol data where only CR and LF matter, and str for general text.Example 3 — splitlines() vs split(b"\n") at the End Most text files end with a newline. Compare how the two methods treat it, and check two edge cases.
log = b"start\nrunning\ndone\n"
print(log.splitlines())
print(log.split(b"\n"))
print(b"".splitlines(), len(b"\n\n".splitlines())) [b'start', b'running', b'done']
[b'start', b'running', b'done', b'']
[] 2 How It Works No phantom line. splitlines() returns exactly the three lines.Extra empty item. split() on b"\n" adds b'' after the final newline.Edge cases. Empty data has no lines, and two newlines mean two empty lines.Frequently Asked Questions Which line endings does bytes.splitlines() recognize? Only \n, \r\n, and \r.
How do I keep the newline bytes? Pass keepends=True: data.splitlines(keepends=True).
Why is there no empty string at the end? splitlines() does not create a line after a final line ending. split(b"\n") does.
Does bytes.splitlines() split on \x0c or \x1c? No. Those are line boundaries only for str.splitlines().
How do I read lines from a binary file? Iterate over the file object, or call data.splitlines() on the bytes you read.
Next: bytes.startswith() Check whether bytes begin with a prefix or any prefix from a tuple.
startswith() → About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
14 people found this page helpful
Helpful Share Copy link Suggestion