Practice Flashcards
Flip to reveal answersWhat are the three file modes and what does each do?
Track your progress — Sign up free to save your progress and get smart review reminders based on spaced repetition.
All 5 Flashcards — File processing
Sign up free to track progress and get spaced-repetition review schedules.
Question
What are the three file modes and what does each do?
Answer
r reads and fails if the file does not exist. w empties the file immediately and then writes, creating it if absent. a appends at the end, keeping what was already there.
Question
What is the danger of opening a file in w mode?
Answer
It empties the file the moment it opens, before anything is written. Choosing w when you meant a destroys the previous contents, and no error is raised because it is a valid operation.
Question
Why must a line read from a file be stripped and converted?
Answer
Every line carries the invisible newline that ended it, so comparisons fail until it is stripped. And everything read is text, so "72" must be converted with int() before any arithmetic.
Question
Why must a file be closed?
Answer
Its contents may not be fully written to disk otherwise, and an open file holds a system resource, possibly locking it against other programs.
Question
What does a with block do for file handling?
Answer
It closes the file automatically when the block ends, even if something fails inside it. It is the finally block of exception handling, built into the language.
Read the notes
Full study notes for File processing
Topic 6.5 hub
File processing
More from Topic 6.5
All flashcards in this topic
Computer Science exam skills
Paper structures & tips
Track your progress with spaced repetition
Sign up free — Aimnova tells you exactly which cards to review and when, so you remember everything before your IB exam.
Start Free