KwickAcademy Python · 9 min · free
Text File Handling: Open, Read, Write and Append
Learn Python text file handling for CBSE Class 12: open(), file modes, with, read(), readline(), readlines(), write(), seek(), tell(). A file is saved on the disk, so its data stays even after the program ends, unlike variables in memory.
Follows the syllabus of: CBSE Class 12 Computer Science (083), Cambridge IGCSE Grade 9 Computer Science (0478), Cambridge IGCSE Grade 10 Computer Science (0478)
On screen in this lesson
Why do we need files?
| Variables live in memory and vanish when the program ends |
| A file is saved on the disk, so it stays |
| Uses: marks lists, attendance, game high scores |
| A text file stores readable characters, line by line |
The six file modes
| Mode | Does | If file missing |
|---|---|---|
| r | read only | FileNotFoundError |
| w | write, erases old | creates it |
| a | add at the end | creates it |
| r+ | read and write | FileNotFoundError |
| w+ | write and read | creates it |
| a+ | add and read | creates it |
Text, binary and CSV files
| File type | Stores | Modes |
|---|---|---|
| Text (.txt) | readable lines | r, w, a |
| Binary (.dat) | raw bytes | rb, wb, ab |
| CSV (.csv) | text with commas | r, w, a + csv |
Why closing matters
| Written text may wait in a buffer until close |
| An open file stays locked for other programs |
| with closes the file even if an error happens |
| Best habit: always use with |
Read and write methods
| Method | Works on | Gives back |
|---|---|---|
| read() | whole file | one string |
| read(n) | next n characters | one string |
| readline() | one line | one string |
| readlines() | all lines | a list |
| write(s) | one string | characters written |
| writelines(L) | list of strings | nothing |
Absolute and relative paths
| Path: the address of a file |
| Absolute: full address, e.g. C:/School/marks.txt |
| Relative: from the current folder, e.g. data/marks.txt |
| Just marks.txt means the current folder |
| ../marks.txt means one folder up |
Quick answers
What is the difference between mode w and mode a?
w erases the old text first; a keeps it and adds at the end.
What does readlines() return?
A list of lines, each still ending with a new line character.
KwickClips from this lesson
Short clips, one idea each. Good for revision the night before.
Why do variables lose data?40 sec
Why did my old lines vanish?45 sec
What does readlines() return?39 sec
What happens when the with block ends?39 sec
What does tell() return?42 secThe full lesson, in text
Hello students, welcome to Kwickprep. You type the marks of forty students into a program. You close it, and every mark is gone. How do we keep data safe after the program ends? Today we will learn to open, read, write and append text files in Python.
First, let us see the problem. Variables are kept in the computer's memory, and that memory is cleared when the program ends. A file is saved on the disk, like a notebook, so the data stays even after you switch off the computer. Schools keep marks lists, attendance and high scores in files. Today we use text files, which store readable characters, one line after another.
Working with a file has three steps: open, use, and close. The open function takes the file name and a mode. A mode is one letter that says what you want to do. Here, the mode w means write. The write method puts text into the file. The backslash n is the new line character, so each student gets a new line. Then we close the file. We open it again with mode r, which means read, and the read method gives back the whole file as one string.
Python has six common modes for text files. Mode r only reads, and if the file is missing you get a file not found error. Mode w writes, but it first erases everything already in the file. Mode a means append, which adds new text at the end and keeps the old text. Mode r plus reads and also writes, but the file must already exist. Mode w plus writes and reads, and it also erases the old text first. Mode a plus adds at the end and also lets you read. If you give no mode at all, Python uses r.
Exams often ask you to tell three kinds of files apart. A text file stores readable characters, line by line, and uses modes like r, w and a. A binary file stores raw bytes, like a photo, which people cannot read directly, so its mode adds the letter b, like r b. A CSV file is a text file whose values are split by commas, and Python reads it with the csv module.
Forgetting close is a very common mistake. The with statement solves it. We write with, open of the file, as f, and a colon. The indented block uses the file. When the block ends, Python closes the file for us. The closed attribute tells us if a file is closed, and here it prints True.
Why is closing so important? Python often keeps written text in a buffer, a waiting area in memory, and saves it fully on close. An open file can also stay locked, so other programs may not use it. The with statement closes the file even when an error stops your code. So the best habit is simple: always use with.
This is the difference exams love to test. First, mode w writes Monday into a new log file. Then we open the same file with mode a and write Tuesday. Append keeps Monday and adds Tuesday below it, so both lines print. Pause and predict. What prints if the second open also used mode w? Only Tuesday, because w wipes the file before writing.
Now the other ways to read. We first write three names. The readline method reads just one line, including its new line character. So it gives Riya. The file remembers where it stopped. Then read of three reads only the next three characters. So it prints A M A, the start of Aman.
The writelines method writes every string from a list. Careful, it does not add new lines by itself, so we put backslash n in each string. The readlines method reads all the lines and gives back a list. Each item in the list still ends with the new line character.
Let us put the five methods side by side. Read with empty brackets reads the whole file as one string. Read with a number reads only that many characters. Readline reads one line as a string. Readlines reads all lines as a list of strings. Write writes one string and gives back how many characters it wrote. Writelines writes a list of strings and gives back nothing.
Here is a program exams often ask. We store a name and marks on each line. A for loop over the file gives one line at a time. The split method breaks a line at spaces, so Riya space eighty eight becomes a list of two strings. Index one is the marks, and int turns it into a number. Plus equals adds it to total. Eighty eight plus seventy five prints one hundred sixty three.
A file has a pointer, a marker that shows where the next read will start. The tell method gives the pointer's position, counted from zero. After reading three characters, S U N, tell gives three. The seek method moves the pointer to a position. Seek of zero jumps back to the start, so reading two characters gives S U. In text files, give seek a position counted from the start.
When a file is in another folder, we give its path. A path is the address of a file. An absolute path is the full address, starting from the drive, like C colon, School, marks. A relative path starts from the folder where your program runs, like data, slash, marks. Only the file name means the file is in the current folder. Two dots mean go one folder up.
Exams also ask you to find the error in file code. Opening a missing file with the mode r gives a file not found error. Reading from a file opened with the mode w gives an unsupported operation error, because that file is not readable. Write of eighty eight gives a type error, because write needs a string. Writing after the file is closed gives a value error.
Some exams ask you to write file handling in pseudocode. Open file, for write, opens the file for writing. Write file puts one line in it, and close file closes it. Then we open it for read. Read file reads one line into the variable Line, and we output it. The idea is exactly the same as in Python.
Let us revise what we learned today. Files keep data safe after the program ends. Mode r reads, mode w erases and writes, and mode a adds at the end. We read with read, readline and readlines, and we write with write and writelines. The with statement closes the file for you. Tell gives the pointer position, and seek moves it. Try the marks program with your own class list.
Courses that teach this
| Course | Unit |
|---|---|
| CBSE Class 12 Computer Science (083) | Computational Thinking and Programming – 2 |
| Cambridge IGCSE Grade 9 Computer Science (0478) | 8. Programming |
| Cambridge IGCSE Grade 10 Computer Science (0478) | 8. Programming |
| Programming All levels Python | Input/Output and File Handling |
Voice-over in this lesson is AI-generated. The script is written and checked by Kajal Ma'am. Boards can revise a syllabus mid-year, so confirm anything you plan around against the official board circular. Keep your passwords, OTPs and ID numbers to yourself — we never ask for them. To reach Kajal Ma'am, use the WhatsApp button; sharing your number there is how we call you back.
Free to watch, no sign-up. Live classes with Kajal Ma'am are the paid course; these lessons stay free either way.

