25

If we need to write a new line to a file we have to code:

file_output.write('Fooo line \n')

Are there any reasons why Python does not have a writeln() method?

SimplyKnownAsG
  • 904
  • 9
  • 26
systempuntoout
  • 71,966
  • 47
  • 171
  • 241
  • 8
    Turn the question around: why do some languages use different functions for output depending on whether you want to include an ending line break or not? This goes back at least as far as pascal, which I learned early on, but I have come to prefer the explicit representation on newlines in the output. – dmckee --- ex-moderator kitten Apr 04 '10 at 20:10
  • You might check out [this answer](http://stackoverflow.com/a/16085543/910963) from another post if you need it – SimplyKnownAsG Jul 19 '15 at 21:00

5 Answers5

25

In Python 2, use:

print >>file_output, 'Fooo line '

In Python 3, use:

print('Fooo line ', file=file_output)
Daniel Stutzbach
  • 74,198
  • 17
  • 88
  • 77
14

It was omitted to provide a symmetric interface of the file methods and because a writeln() would make no sense:

  • read() matches write(): they both operate on raw data
  • readlines() matches writelines(): they both operate on lines including their EOLs
  • readline() is rarely used; an iterator does the same job (except for the optional size param)

A writeline() (or writeln()) would be essentially the same as write(), since it wouldn't add an EOL (to match the behavior of writelines()).

The best way to mimic a print to a file is to use the special print-to-file syntax of Python 2.x or the file keyword argument of the print() function, like Daniel suggested.

Personally, I prefer the print >>file, ... syntax over file.write('...\n').

efotinis
  • 14,565
  • 6
  • 31
  • 36
  • 5
    @eftinis, since `readline` exists, your statement of "symmetry" doesn't really hold. The rare use of a `readline` method also doesn't preclude its benefit in certain situations (for example parsers which might need to read a line or two ahead). – SimplyKnownAsG Jul 19 '15 at 20:51
  • Excellent observation @SimplyKnownAsG. The main argument about symmetry is simply not true! – Richard Klassen Nov 08 '20 at 18:19
5

I feel that it should. Nearest thing you can use is:

file_output.writelines([foo_msg, '\n'])
Daniel Reis
  • 12,944
  • 6
  • 43
  • 71
  • 1
    In my experience, this works better and I think it's more safe than doing `out.write(foo_msg+'\n')`. Maybe some folk expert of Python I/O subsystem could confirm that. – loretoparisi Mar 18 '19 at 11:25
4

print() is the function you are looking for. Like much of the core polymorphism in python, printing is provided by a built-in function instead of using methods (just like len(), next(), repr() etc!).

The print() function being the universal interface also makes it more versatile, without the file objects themselves having to implement it. In this case, it defaults to terminating by newline, but it can be chosen in the function call, for example:

print("text", file=sys.stderr, end="\n")

In your suggested use case, all file objects would have to implement not only a .write() method (now used by print()), but also .writeln(), and maybe even more! This function-based polymorphism makes python very rich, without burdening the interfaces (remember how duck typing works).

Note: This model has always been at the center of Python. It is only more pure in my examples, that pertain to Python 3

u0b34a0f6ae
  • 48,117
  • 14
  • 92
  • 101
0

Probably no real reason, just that it's really easy to write a newline when you need it, so why clutter up the file interface with an extra method just to do that?

Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662
  • Do you usually use the syntax used in my question? – systempuntoout Apr 04 '10 at 19:47
  • Python's `print` function prints a new line at the end of the string by default. – Samir Talwar Apr 04 '10 at 20:03
  • 2
    To answer your intended-to-be-rhetorical question: efficiency. Consider (1) `f.write(stuff); f.write('\n') (2) `f.write(stuff + '\n')` (3) `f.writeln(stuff)` – John Machin Apr 05 '10 at 00:16
  • 1
    It seems unlikely to me that while doing file I/O the function calls will affect the overall efficiency. Eventually the bytes will get written somewhere, and that I/O is going to be the bottleneck, not an extra call to a write function. There's also something to be said for keeping interfaces lean. – Ned Batchelder Apr 05 '10 at 00:27