0

When I use f.writelines('aaa') in both Python2 and Python3, the string aaa is written to the file, but what I expect is three separate lines of a.

Here is the document of writelines:

writelines(sequence_of_strings) -> None.  Write the strings to the file.

Note that newlines are not added.  The sequence can be any iterable object 
producing strings. This is equivalent to calling write() for each string.

Please note that it accepts any iterable and str is iterable, so why instead of writing a line for each item in the string, writelines() simply writes a single string?

Should I need to use f.writelines(list('aaa')) to get the desired result?

I wonder whether this is a deliberately consideration or simply a novel bug.

cgsdfc
  • 538
  • 4
  • 19
  • Your analysis and expectation assume that `writelines` calls `list(...)` on the string that is given, isn't it? – DeepSpace May 21 '18 at 10:02
  • While the question itself is not exactly a duplicate, you may find this answer interesting: https://stackoverflow.com/a/12377575/1453822 – DeepSpace May 21 '18 at 10:03
  • _Note that newlines are not added._ Does that tell you anything? – zipa May 21 '18 at 10:04
  • @DeepSpace Yes. – cgsdfc May 21 '18 at 11:50
  • @DeepSpace Oh I figure out. 3 ``a`` without newline is just ``aaa``. – cgsdfc May 21 '18 at 11:56
  • Folks, sorry first for asking such a stupid question. If I have known that earlier but the name of ``writelines`` makes me believe that it can turn a sequence of strings into a bunch of lines. Now I am clear about the nature of ``writelines``. – cgsdfc May 21 '18 at 12:01

1 Answers1

0

from documentation

writelines(lines)

Write a list of lines to the stream. Line separators are not added, so it is usual for each of the lines provided to have a line separator at the end.

Example:

with open('test.txt', 'w') as f:
    f.writelines(['a\n','a\n','a\n'])

Output:

a
a
a

Please note that the line separator \n is part of each string.

Edwin van Mierlo
  • 2,398
  • 1
  • 10
  • 19
  • What would be the output of `f.writelines('a\na\na')` ? – DeepSpace May 21 '18 at 10:04
  • 2
    @DeepSpace you probably knew the answer before you posted the comment, and so do/did I... the output will be the same, also it would be the same as `f.write('a\na\na')`. Or should I say 'similar' as your example does not have a `\n` (nor does my example in this comment) so technically the code in my answer has 1 more `\n` at the end. – Edwin van Mierlo May 21 '18 at 10:08
  • Why would you ever do that? If you had newlines in a string, you would just write the string using `f.write`. – Sean Breckenridge May 21 '18 at 10:08
  • @SeanBreckenridge... you are correct by asking that, and to be honest I wouldn't, I am only answering the OP question, who did not include line separators, while the documentation clearly states that the `.writelines()` does not add them, hence this answer. – Edwin van Mierlo May 21 '18 at 10:10
  • @EdwinvanMierlo ah my bad. I should have @DeepSpace 'd there. I was asking why we would ever do `f.writelines('a\na\na')` – Sean Breckenridge May 21 '18 at 10:11
  • 1
    @SeanBreckenridge This whole question is hypothetical and so was my bite – DeepSpace May 21 '18 at 10:13
  • @DeepSpace well... maybe not hypothetical, if you are creating a generic function in your code which can take lists as well as strings to write to file (albeit the elements of the list as well as the string must have a line separator), then you could use writelines() in your function to cater for both easily... right ? – Edwin van Mierlo May 21 '18 at 10:18
  • @EdwinvanMierlo Fair enough, I guess – DeepSpace May 21 '18 at 10:21