Practice Flashcards
What 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 Flashcards in Topic 6.5
Below are all 5 flashcards for this topic. Sign up free to track your progress and get personalized review schedules.
6.5.15 cards
What are the three file modes and what does each do?
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.
What is the danger of opening a file in w mode?
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.
Why must a line read from a file be stripped and converted?
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.
Why must a file be closed?
Its contents may not be fully written to disk otherwise, and an open file holds a system resource, possibly locking it against other programs.
What does a with block do for file handling?
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.
Topic 6.5 study notes
Full notes & explanations for File processing
Computer Science exam skills
Paper structures, command terms & tips
Want smart review reminders?
Sign up free to track your progress. Our spaced repetition algorithm will tell you exactly which cards to review and when.
Start Free