python – Writelines writes lines without newline, Just fills the file

python – Writelines writes lines without newline, Just fills the file

This is actually a pretty common problem for newcomers to Python—especially since, across the standard library and popular third-party libraries, some reading functions strip out newlines, but almost no writing functions (except the log-related stuff) add them.

So, theres a lot of Python code out there that does things like:

fw.write(n.join(line_list) + n)


fw.write(line + n for line in line_list)

Either one is correct, and of course you could even write your own writelinesWithNewlines function that wraps it up…

But you should only do this if you cant avoid it.

Its better if you can create/keep the newlines in the first place—as in Greg Hewgills suggestions:

line_list.append(new_line + n)

And its even better if you can work at a higher level than raw lines of text, e.g., by using the csv module in the standard library, as esuaro suggests.

For example, right after defining fw, you might do this:

cw = csv.writer(fw, delimiter=|)

Then, instead of this:

new_line = d[looking_for]+|+|.join(columns[1:])

You do this:

row_list.append(d[looking_for] + columns[1:])

And at the end, instead of this:


You do this:


Finally, your design is open a file, then build up a list of lines to add to the file, then write them all at once. If youre going to open the file up top, why not just write the lines one by one? Whether youre using simple writes or a csv.writer, itll make your life simpler, and your code easier to read. (Sometimes there can be simplicity, efficiency, or correctness reasons to write a file all at once—but once youve moved the open all the way to the opposite end of the program from the write, youve pretty much lost any benefits of all-at-once.)

The documentation for writelines() states:

writelines() does not add line separators

So youll need to add them yourself. For example:

    line_list.append(new_line + n)

whenever you append a new item to line_list.

python – Writelines writes lines without newline, Just fills the file

As others have noted, writelines is a misnomer (it ridiculously does not add newlines to the end of each line).

To do that, explicitly add it to each line:

with open(dst_filename, w) as f:
    f.writelines(s + n for s in lines)

Leave a Reply

Your email address will not be published. Required fields are marked *