File Handling in Python
Class 12 · Computer Science
4 ideas in Class 12 Computer Science, explained in plain words, with games and quizzes to practise.
Text Files versus Binary Files
Part of: Files and opening them
A file is a named spot on secondary storage where data stays for later use. A text file holds readable characters and opens in any simple editor. A binary file holds bytes that are not character codes, such as pictures or sound, so it needs its own program and shows garbage in a plain editor.
Why it matters. Choosing the right file kind matters in every project. Learners who know the difference stop worrying when a picture looks strange in an editor, and they learn why a decorated document is far bigger than the same words saved as plain text.
Key words
- File
- : A named place on secondary storage where data is kept for later use.
- Text File
- : A file of readable characters that any simple editor can open.
- Binary File
- : A file of non-readable bytes that needs its own program to open.
- End of Line
- : A special mark that tells the reader where one line ends.
- Secondary Storage
- : A device like a disk where data stays after the program ends.
Try it in two minutes
- Save your name in a plain text file and note its small size.
- Save the same name in a decorated document and compare the two sizes.
- Ask your child why the bigger file needs more than the letters alone.
Try at home. Ask your child to find one text file and one picture file at home and predict what each shows in a plain editor.
Opening and Closing Files
Part of: Files and opening them
The open step connects a program to a file and gives back a file object for reading and writing. The mode decides the job: reading keeps contents safe, writing erases and starts fresh, and appending adds at the end. Closing frees memory and pushes buffered data to disk, and a with block closes the file by itself.
Why it matters. Wrong modes destroy data, such as opening a diary in write mode and losing old lines. Learners who pick modes carefully and always close files protect data and avoid half-written files after errors.
Key words
- File Object
- : The link returned on opening, used to move data to and from the file.
- Read Mode
- : Opening that starts at the beginning and keeps contents unchanged.
- Write Mode
- : Opening that erases old contents and starts fresh.
- Append Mode
- : Opening that starts at the end so new data follows old data.
- With Block
- : An opener that closes the file by itself, even after an error.
Try it in two minutes
- Open a practice file in read mode and note that old lines stay safe.
- Open a copy in write mode and see that old lines vanish.
- Repeat with a with block and check that closing happens by itself.
Try at home. Ask your child to predict, then test, what each mode does to the same practice file.
Writing and Reading Text Files
Part of: Reading, writing and pickling
The write method stores one piece of text and reports how many characters went in, so numbers must first be turned into text. The writelines method stores a whole sequence of strings at once. For reading, the read method grabs a fixed number of bytes, the readline method grabs one line, and the readlines method returns every line as a list.
Why it matters. Reports, logs and stored answers all depend on writing and reading text correctly. Learners who master these methods can save work permanently and fetch exactly the part they need instead of retyping everything.
Key words
- write Method
- : A call that stores one piece of text and reports its length.
- writelines Method
- : A call that stores a whole sequence of strings at once.
- read Method
- : A call that grabs a fixed number of bytes from a file.
- readline Method
- : A call that grabs one full line up to the line mark.
- readlines Method
- : A call that returns every line as items of a list.
Try it in two minutes
- Write one greeting line and note the reported character count.
- Read back only the first ten bytes and compare with the full line.
- Fetch all lines as a list and count how many items it has.
Try at home. Ask your child to save three favourite lines in a file and read back only the first line.
Moving Around and Pickling
Part of: Reading, writing and pickling
The tell method reports the current byte position counted from the file start, and the seek method jumps to a chosen spot. Pickling saves whole Python objects, like lists, as byte streams in binary files: the dump method stores them and the load method rebuilds them. Reading past the last stored object raises an end-of-file error that a loop can catch.
Why it matters. Big files cannot be reread from the start every time, so jumping saves effort. Pickling lets games and registers save full records and resume later. Learners who use these tools handle real data stores instead of tiny practice files.
Key words
- tell Method
- : A call that reports the current byte position from the file start.
- seek Method
- : A call that jumps the file object to a chosen byte spot.
- Pickling
- : Turning a Python object into a byte stream for storage.
- dump Method
- : A call that stores a pickled object into a binary file.
- load Method
- : A call that rebuilds a pickled object from a binary file.
Try it in two minutes
- Write one line, jump back to the start and read the first ten bytes.
- Store a small list with the dump method and close the file.
- Load it back and check that the rebuilt list matches.
Try at home. Ask your child to store a small score list with pickling and load it back to prove nothing changed.
What your child will practise
- Moving Around and Pickling: Space Quick Fire (Quick Fire)
- Opening and Closing Files: Candy Puzzle (Clue Puzzle)
- Opening and Closing Files: Ocean Quick Fire (Quick Fire)
- Text Files versus Binary Files: Jungle Quick Fire (Quick Fire)
- Writing and Reading Text Files: Space Puzzle (Clue Puzzle)
- Quiz: Moving Around and Pickling (8 questions)
- Quiz: Opening and Closing Files (8 questions)
- Quiz: Text Files versus Binary Files (8 questions)
- Quiz: Writing and Reading Text Files (8 questions)