Writing string to a file on a new line every time
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Writing each string on a new line requires appending a newline character (\n) after each string. In Python, the simplest approach is using print() with a file argument (it adds \n automatically) or write() with an explicit \n. Other languages follow similar patterns. The key is understanding whether your write function adds a newline for you or whether you need to add it manually.
Python
Using write() with \n
Using print() with file parameter
Using writelines()
Joining with newlines
Appending to an existing file
JavaScript (Node.js)
Java
Go
C#
Platform-Specific Newlines
Common Pitfalls
- Forgetting that
writelines()does not add newlines: Python'swritelines()writes each element exactly as-is without inserting newlines between them. You must append\nto each string yourself, or the entire output ends up on one line. - Extra blank line at the end: Writing
"\n".join(lines) + "\n"adds a trailing newline. Some tools expect no trailing newline. Omit the+ "\n"if the file should not end with a blank line. - Using
"w"mode when you meant"a": Opening a file with"w"truncates it to zero length before writing. Use"a"(append) to add to an existing file without erasing its contents. - Mixing
\nandos.linesepin text mode: In Python's text mode,\nis automatically translated to the platform line ending. Usingos.linesepin text mode on Windows produces\r\r\n(double carriage return). Use\nin text mode andos.lineseponly in binary mode. - Not closing the file / not using
with: Forgetting to close a file (or not using awithstatement) can leave data in the write buffer unflushed. The last few lines may be lost if the program crashes before the buffer is flushed.
Summary
- Use
f.write(line + "\n")orprint(line, file=f)in Python writelines()does not add newlines — you must add them manually- Use
"a"mode to append without overwriting the file - Use
\nin text mode (Python translates it to the platform line ending) - In Java,
Files.write(path, lines)andPrintWriter.println()handle newlines automatically - Always use
withstatements ortry-finallyto ensure files are properly closed and flushed
Related reading
- __init__ got an unexpected keyword argument 'cachedir' when importing top2vec
- Algorithm for autocomplete?
- Algorithm for re-wrapping hard-wrapped text?
- Algorithm for separating nonsense text from meaningful text
- Writing Unicode text to a text file?
- Writing unit tests in Python How do I start?
- Algorithm or C library for identifying 'keywords' in a set of messages?
- Algorithm to compare similarity of English sentences
.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 courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.