Cheat sheet · Class 12 · CBSE · Science Computer Science

File Handling in Python cheat sheet

The whole chapter on one page: key formulas, facts and the points to remember, for quick revision before a test.

CBSE Class 12 · Computer Science

Cheat sheet

Chapter 4File Handling in Python

Open → read/write → close (or use with, which closes for you).

1. Modes

ModeMeaning
r / rbread (file must exist)
w / wbwrite (creates or overwrites)
a / abappend
r+ / w+ / a+read and write

2. Text Files

  • read(n), readline(), readlines().
  • write(str), writelines(list).
  • with open('f.txt') as f: …
  • tell() gives position; seek(offset, from) moves it.

3. Binary Files (pickle)

  • pickle.dump(obj, f) — serialise.
  • pickle.load(f) — deserialise; EOFError at the end.

4. CSV Files

  • csv.writer(f).writerow(row) / writerows(rows).
  • csv.reader(f) gives rows as lists.
  • Open with newline='' when writing.

5. Key Points

  • Text files store characters; binary files store bytes.
  • Absolute vs relative paths.
  • flush() writes the buffer.
  • w mode erases existing content.

Remember: Modes | read/write methods | pickle | csv

File Handling in Python: questions and notes

More Class 12 · CBSE · Science Computer Science cheat sheets

Practise File Handling in Python

Sign up free to practise the full chapter. Questions you get wrong come back until you've mastered them.

Start practising free

Free to start · No credit card needed