Python Essentials for AI Engineer

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

Python
rows = ["T1,499.00", "T2,120.50"]with open("out.txt", "w", encoding="utf-8") as f:    n = f.write("txn_id,amount\n")    f.writelines(row + "\n" for row in rows)    # writelines adds NO newlines itself    print("total", 619.5, file=f)               # print adds the newlineprint(n)                                         # 14 -> characters writtenwith open("out.txt", encoding="utf-8") as f:    print(f.read().splitlines())# ['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:

Python
import csv, osresults = [("T1", "Refund issued, customer happy"), ("T2", 'Asked "where is my order?"\nEscalated')]bad = "".join(f"{tid},{text}\n" for tid, text in results)print(bad.count("\n"), "lines for 2 rows")              # 3 lines for 2 rows -> broken CSVtmp = "summaries.csv.tmp"with open(tmp, "w", encoding="utf-8", newline="") as f:    writer = csv.writer(f)    writer.writerow(["txn_id", "summary"])    writer.writerows(results)os.replace(tmp, "summaries.csv")                         # atomic swapwith open("summaries.csv", encoding="utf-8", newline="") as f:    back = list(csv.reader(f))print(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 does writelines(). Add \n yourself or use print(..., 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="" with csv?" — So the csv module controls line endings itself; otherwise Windows gets extra blank rows and embedded newlines can be mangled.