Back to Topic 6.5 — File processing
6.5.1Computer Science SL5 flashcards

File processing

Practice Flashcards

Flip to reveal answers
Card 1 of 56.5.1
6.5.1
Question

What are the three file modes and what does each do?

Click to reveal answer

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.

Card 1definition

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.

Card 2concept

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.

Card 3process

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.

Card 4concept

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.

Card 5definition

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.

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