Writelines writes lines without newline, Just fills the file
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.
Introduction
Python's writelines() method writes a sequence of strings to a file without adding newline characters between them. Unlike what the name suggests, writelines() does not write separate "lines" — it writes each string exactly as-is, concatenated together. If you want each string on its own line, you must include \n in each string yourself. This is a deliberate design choice: writelines() is the inverse of readlines(), which preserves the newline characters already present in each line.
The Problem
All three strings are concatenated into a single line because none of them contain a newline character.
The Fix: Add Newlines to Each String
Why writelines() Works This Way
writelines() is designed as the counterpart to readlines(). When you read a file with readlines(), each line already includes its trailing \n:
The symmetry between readlines() and writelines() means data round-trips correctly without adding or removing newlines.
writelines() vs write() vs print()
print() adds a newline by default (controlled by the end parameter), making it the simplest option for writing lines to a file.
Writing Lines with a Loop
For more control, use a loop with write():
Performance Comparison
For large files, "\n".join(lines) is typically fastest because it makes a single system call, but it requires all data in memory at once. writelines() with a generator is the most memory-efficient approach.
Writing Binary Files
writelines() also works with binary files, but uses bytes instead of str:
Common Pitfalls
- Assuming
writelines()adds newlines: The name is misleading.writelines()does not add any separator between strings. It writes each string exactly as provided. Always include\nin each string if you want separate lines. - Passing a single string instead of an iterable:
writelines("hello")writes each character individually (h,e,l,l,o) because a string is iterable. This works but is confusing — usewrite("hello")for single strings. - Double newlines from
readlines()+ manual\n: If you read lines withreadlines()(which preserves\n) and then add another\nbefore writing, each line gets a blank line between it. Check whether your strings already end with\nbefore adding more. - Memory issues with large
join()operations:"\n".join(million_lines)creates a single huge string in memory. For very large files, usewritelines()with a generator expression to write incrementally without buffering everything. - Forgetting the final newline: POSIX convention expects files to end with a newline. Using
"\n".join(lines)without adding a trailing\nproduces a file where the last line has no newline, which can cause issues with tools likewc -lthat expect it.
Summary
writelines()writes strings exactly as given — it does not add newlines between them- Add
\nto each string yourself:f.writelines(line + "\n" for line in lines) writelines()is the inverse ofreadlines(), which preserves trailing\ncharacters- For simple line-by-line writing,
print(line, file=f)is the easiest approach - For large files,
writelines()with a generator is the most memory-efficient "\n".join(lines)with a singlewrite()is fastest but uses more memory
Related reading
- Writing a list to a file with Python, with newlines
- Writing a list to a file with Python, with newlines
- Writing a pandas DataFrame to CSV file
- Writing a pandas DataFrame to CSV file
- Writing a pickle file to an s3 bucket in AWS
- Writing a Python list of lists to a csv file
- Writing an asynchronous process that can be awaited
- Writing string to a file on a new line every time
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.