Course Content
Python Essentials for AI Engineer
6 sections · 48 lessons
How do you write data to a file?
What you need to know
The basics
1rows = ["T1,499.00", "T2,120.50"]2with open("out.txt", "w", encoding="utf-8") as f:3 n = f.write("txn_id,amount\n")4 f.writelines(row + "\n" for row in rows) # writelines adds NO newlines itself5 print("total", 619.5, file=f) # print adds the newline6print(n) # 14 -> characters written7with open("out.txt", encoding="utf-8") as f:8 print(f.read().splitlines())9# ['txn_id,amount', 'T1,499.00', 'T2,120.50', 'total 619.5']Buffering
write usually puts data into a memory buffer, not straight onto disk. The buffer is written out when it fills up, when you call f.flush(), or when the file is closed. If a process crashes before that, the last writes are lost. For a live log that another program watches, call flush() after important lines.
The csv module
Text that contains commas, quotes or newlines breaks hand-made CSV. csv.writer quotes such fields for you. Open the file with newline="", as the csv docs require, so the module controls line endings and you don't get blank rows on Windows.
Atomic writes
If a job crashes halfway through rewriting predictions.csv, readers see a half-written file. The safe pattern is to write a temporary file in the same folder and then call os.replace(tmp, final), which swaps it in in one step — readers see either the old file or the new one, never a mix.
A real-life example
You save LLM-generated summaries to CSV for the operations team. Some summaries contain commas and line breaks:
1import csv, os23results = [("T1", "Refund issued, customer happy"), ("T2", 'Asked "where is my order?"\nEscalated')]45bad = "".join(f"{tid},{text}\n" for tid, text in results)6print(bad.count("\n"), "lines for 2 rows") # 3 lines for 2 rows -> broken CSV78tmp = "summaries.csv.tmp"9with open(tmp, "w", encoding="utf-8", newline="") as f:10 writer = csv.writer(f)11 writer.writerow(["txn_id", "summary"])12 writer.writerows(results)13os.replace(tmp, "summaries.csv") # atomic swap1415with open("summaries.csv", encoding="utf-8", newline="") as f:16 back = list(csv.reader(f))17print(len(back), back[2][1].splitlines()[0]) # 3 Asked "where is my order?"The hand-built version gives three lines for two rows, and the first row has three columns instead of two, and Excel shows the text split across cells. The csv version quotes the tricky fields, reads back as exactly one header and two rows, and the os.replace means the dashboard never reads a half-written file.
Follow-up questions to expect
- "Does
write()add a newline?" — No. Neither doeswritelines(). Add\nyourself or useprint(..., file=f). - "When is data actually on disk?" — After a flush or close, and even then the OS may cache it briefly;
os.fsync(f.fileno())forces it to the device for critical data. - "Why
newline=""withcsv?" — So thecsvmodule controls line endings itself; otherwise Windows gets extra blank rows and embedded newlines can be mangled.