Learn › Class 12 › Computer Science / Informatics Practices › File handling and data structures (stack)
File handling
Work it out on paper first. Then check the hint or the answer.
1. Which mode adds text to the end of an existing file without erasing it?
Hint
Think of 'append'.
Answer and explanation
Answer: 'a'
Append mode writes at the end and keeps old content.
2. A text file holds 'Hello' on its first line. After f = open(...) and f.read(5), what does f.tell() return?
Hint
Five characters have been read.
Answer and explanation
Answer: 5
For plain ASCII text, the position is 5 after reading 5 characters.
3. A text file has 4 lines. What is len(f.readlines())?
Hint
readlines() returns one list item per line.
Answer and explanation
Answer: 4
The list has one string for each of the 4 lines.
4. Which exception does pickle.load() raise when no objects are left in the file?
Hint
EOF means 'end of file'.
Answer and explanation
Answer: EOFError
Loops that read pickled records usually stop by catching EOFError.