Python · The language, not just syntax
How do you read and write files safely in Python?
Short answer
Open files inside a with block, specify encoding explicitly for text, and stream large files rather than reading them whole. Use pathlib for path manipulation and atomic replace for critical writes.
The open function returns a file object whose lifetime should be tied to a with block so the file is closed even on error. Text mode decodes bytes using an encoding, while binary mode returns bytes unchanged.
from pathlib import Path
source = Path("data/events.csv")
target = source.with_suffix(".tmp")
with source.open("r", encoding="utf-8", newline="") as reader:
for line in reader:
process(line)
with target.open("w", encoding="utf-8", newline="") as writer:
writer.write("id,total\n")
target.replace(source)Rules for production code
- Always pass encoding for text files; the default depends on the platform
- Use newline="" for CSV so the csv module controls line endings
- Iterate the file object for line-by-line reading instead of read().splitlines()
- Validate and normalise paths before opening user-supplied filenames
No buffering or flushing assumption should be load-bearing. If a process crashes, data in user-space buffers can be lost. For durable writes, flush and fsync before considering the write complete, and use a temporary file plus rename for atomicity.
Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1What encoding should you use and why be explicit?
- 2How do you read a very large file without exhausting memory?
- 3How would you write a file atomically?
- 4What are the risks of joining user input into a file path?
Say it in the interview
I always open files in a with block and specify encoding explicitly for text. For large files I iterate line by line instead of reading everything, and for critical writes I write to a temporary file and replace the target so readers never see a partial file. I also validate user-supplied paths against a base directory to prevent traversal. Buffering means I do not assume data is durable until flush and fsync complete.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live Python mock interview and get scored feedback on depth, clarity and confidence.