Having a `writeline` method on appropriate `io` objects

Writing a string terminated with a newline seems to be a very common operation in file I/O, so a writeline convenience method seems like it would be an useful addition to the language. From a quick “what do you think” poll of acquaintance who use python it seems that the reactions to this idea were mostly positive.

It would be a very simple method that does something along the lines of


where the implementation could be:

  • newline is the newline parameter of the given IO object, or
  • writeline has signature of (data, *, newline=None) where a value of None would default to the newline parameter, or use the str/bytes passed.

The current most popular method for achieving this is using any method of string concatenation with write (or writelines for multiple strings) or an additional call to write that appends a newline. I’ve taken the liberty of searching the CPython GitHub repository for some real examples of this use.¹

Another real current alternative is using print(text, end=..., file=file), but I have not seen this done very often.

I would also imagine this has been talked about on the mailing list before, but I could not find any useful results from either Google or DDG.


These examples are not by any means exhaustive of the standard library alone (I only skimmed through Lib/[a-n]) and this doesn’t include examples where strings are concatenated beforehand and then written out such as this demonstrative code I pulled out of thin air

output = ''
for lineno, line in enumerate(lines, start=1):
    line = process(line)
    output += f'{lineno} - {line}\n'

Another real current alternative is using print(text, end=..., file=file) , but I have not seen this done very often.

A newline is the default for end, so it’s just print(text, file=file)

This has the advantage of already being in the language. You won’t need to wait for Python 3.9 (the earliest a new feature can get into the standard library), and for updates to all the other file-like classes ever written.

The argument of not seeing it very often applies to the proposed writeline as well.

Yes, but it doesn’t default to the newline parameter on the io object passed in as file, so the ... was to indicate one can change the value if desired

I do not understand what you mean, of course it is not seen often if it does not exist at all.