Core9X
Log inSign up

MBOSE Class XII Computer Science · Chapter 2

File Handling in Python

Opening soon

Learn to read and write text, binary and CSV files in Python, so your programs can save and load data.

17 lessons · 56 practice questions · unit "Computational Thinking and Programming" carries 40 marks

See classes open nowLog inMBOSE Class XII opens to new students soon.

What this chapter covers

  • Text, binary and CSV files
  • Opening and closing files
  • Access modes
  • With clause
  • Write() and writelines()
  • Read(), readline() and readlines()
  • Tell() and seek()
  • Text-file programs
  • The pickle module — dump() and load()
  • Binary-file programs
  • The csv module — writer(), writerow(), writerows(), reader()

Key terms

Text file

File of human-readable characters

Binary file

File of raw bytes, not human-readable

CSV file

Text file of comma-separated records

File object

Handle returned by open() that links program and file

Access mode

Purpose for which a file is opened (r, w, a, b, +)

EOL

End-of-line character, \n

File pointer

Position where the next read or write happens

Pickling

Converting an object into a byte stream (serialization)

Unpickling

Converting the byte stream back (deserialization)

Delimiter

Character separating fields in a CSV file

Important questions with answers

10 of the 56 questions on this chapter, solved.

  1. Question 1 · Multiple choice · 1 mark

    The default mode of open() is

    • (a) 'r'
    • (b) 'w'
    • (c) 'a'
    • (d) 'r+'

    Answer: (a) 'r'

    Read, text mode.

  2. Question 2 · Multiple choice · 1 mark

    open() returns

    • (a) a string
    • (b) a list
    • (c) None
    • (d) a file object

    Answer: (d) a file object

    Also called a file handle.

  3. Question 3 · Multiple choice · 1 mark

    f.seek(0)

    • (a) moves the pointer to the beginning
    • (b) moves it to the end
    • (c) deletes the file
    • (d) closes the file

    Answer: (a) moves the pointer to the beginning

    Offset 0 from the start.

  4. Question 4 · Multiple choice · 1 mark

    pickle.dump() is used to

    • (a) write an object to a binary file
    • (b) read an object
    • (c) delete a file
    • (d) open a file

    Answer: (a) write an object to a binary file

    load() reads it back.

  5. Question 5 · Assertion and reason · 1 mark

    Assertion (A): Opening an existing file in 'w' mode erases its contents.

    Reason (R): In 'w' mode the file is overwritten from the beginning.

    • (a) Both A and R are true, and R is the correct explanation of A.
    • (b) Both A and R are true, but R is not the correct explanation of A.
    • (c) A is true, but R is false.
    • (d) A is false, but R is true.

    Answer: (a) Both A and R are true, and R is the correct explanation of A.

    Use 'a' to keep the contents.

  6. Question 6 · Short answer · 2 marks

    Distinguish 'w' and 'a' modes.

    Answer

    'w' erases the old contents and writes from the beginning; 'a' keeps them and writes at the end.

  7. Question 7 · Short answer · 2 marks

    What is a delimiter?

    Answer

    The character that separates fields in a CSV file (comma by default).

  8. Question 8 · Short answer · 3 marks

    What does f.seek(10) followed by f.read(5) do?

    Answer

    Moves to the 11th byte and reads the next 5 characters.

  9. Question 9 · Long answer · 5 marks

    Explain the file access modes with examples.

    Answer

    Section 1.2; Section 8.2 (HSSLC 2021, Q6(d) (second choice)).

  10. Question 10 · Long answer · 5 marks

    Explain pickling with a program to write and read records in a binary file.

    Answer

    Section 1.7; Section 8.2 (HSSLC 2023, Q9(b) (first choice)).

Study the whole chapter

17 step-by-step lessons, all 56 practice questions with hints, flashcards and mock tests for MBOSE Class XII Computer Science.

See classes open nowLog inMBOSE Class XII opens to new students soon.
File Handling in Python: MBOSE Class 12 Computer Science notes, important questions and answers · Core9X