Introduction to Files
Every program you have written so far has the same flaw: the moment it ends, everything it knew is gone. A file is how a program keeps something. This lesson is about what a file actually is underneath — and why the same computer holds two very different kinds of them.
1Why a program needs files at all
Think about a marks program. You run it, type in five subjects, and it prints the average. Run it again tomorrow and you type all five in again. The program did not remember anything — it never could.
Everything a running program holds — every variable, every list, every value input() collected — lives in RAM, the computer's working memory. RAM is fast, and it is temporary. When the program ends, the space is handed back and the values are wiped.
- Holds your variables and lists
- Very fast
- Emptied when the program ends
- Emptied when the power goes
- Holds whatever you wrote into it
- Slower to reach
- Still there after the program ends
- Still there after a restart
A file is a named collection of data stored permanently on a storage device — a hard disk, an SSD, a pen drive — so that it survives after the program that made it has finished.
The two words worth keeping are named and permanent. Named, because a program has to be able to ask for it back by name. Permanent, because that is the entire point: this is data that outlives the program.
2Underneath, every file is a row of numbers
Here is the fact that makes the rest of this chapter make sense. A disk cannot store letters, colours or sound. It can only store bytes — whole numbers from 0 to 255. That is all.
So a file, any file, is one long row of numbers. A photo is a row of numbers. A song is a row of numbers. Your Python program is a row of numbers. What separates them is not the numbers — it is what those numbers are meant to stand for.
The same idea as a radio
3Text files: the numbers are character codes
In a text file, every byte is a character code — the same codes you met with ord() and chr() in Class 11. Byte 72 is H, byte 101 is e, byte 10 is the newline you get by pressing Enter.
Those five numbers — 72, 101, 108, 108, 111 — are exactly what would be sitting on the disk if you saved Hello into a file called notes.txt. Nothing else goes in: no font, no size, no colour, no bold. A text file has no room for any of that, because it has nowhere to put it.
- Readable by a human. Open it in Notepad and you see what you wrote.
- Made of lines. A newline character (
\n) marks the end of each one. - Editable anywhere. Notepad, IDLE, VS Code — any of them can open it, because they all agree on what the codes mean.
- Common extensions:
.txt,.py,.csv,.html.
4Binary files: the numbers mean something else
In a binary file, the bytes are not character codes. They are whatever the program that created the file decided they should be — the red-green-blue values of a pixel, the loudness of a millisecond of sound, a compressed block of text, a machine instruction.
Try to read those bytes as characters anyway and you get nonsense, because you are asking the wrong question of them. Byte 137 is not a letter. Nobody typed it, and no letter will come out of it.
- Not readable by a human. Open it in Notepad and you get symbols, boxes and blanks.
- Not made of lines. There is no such thing as “line 3” of a photo.
- Readable only by a program that knows the format. A photo viewer knows what a
.png's bytes mean. Notepad does not. - Common extensions:
.png,.jpg,.pdf,.docx,.mp3,.exe.
5Look inside five real files
You have almost certainly done this by accident: right-clicked a photo, chosen “Open with → Notepad”, and got a screenful of junk. Here is the same experiment, done on purpose. The left row is what is really on the disk; the right row is what a text editor makes of it.
Every byte is a character code, written here in hex the way a file viewer shows them. 48 in hex is 72, and 72 is 'H'. 0A is 10, the newline you get by pressing Enter. Nothing else is in the file — no font, no size, no colour.
6CSV files — which are text files
CSV stands for Comma Separated Values. It is how a table is stored when you want any program to be able to read it. One line per row, and the values in that row separated by commas:
name,marks
Asha,92
Rohit,78
Meera,85The first line is usually the header — the column names. Every line after it is one record. Open this in Excel and you get a neat grid with three columns; open it in Notepad and you get exactly the four lines above, because that is all the file contains.
The syllabus lists three types of file: text, binary and CSV. That is worth reading carefully, because it is easy to come away thinking there are three separate things. There are two:
CSV gets a name of its own because of what people do with it, not because of how it is stored.
.csv is a text file — just commas and newlines. An .xlsx is a binary file, and it has to be, because it also stores formulas, colours, fonts, charts and multiple sheets. A CSV can hold none of those. It holds values, and that is the trade: it loses the formatting and gains the ability to be read by anything.7Almost every file you meet is binary
This surprises people. Text files feel like the normal case, because they are the ones you can look at. In fact they are the minority. Nearly everything on a real computer — every document, every photo, every song, every installed program — is binary.
Count them: four text, eight binary — and that is a generous sample. On a real laptop the ratio is far more lopsided, because every single file that makes Windows work is binary too.
A Word document is the clearest case. It looks like text; it is text you typed. But the file has to store your text and the font, the size, the bold, the margins, the page breaks, the pictures you pasted in. None of that fits in a row of character codes, so .docx is binary — a zip folder, in fact, with your writing compressed inside it.
.docx, rename it to .zip and open it. Inside you will find a folder of files — and one of them, document.xml, holds your actual words. Rename it back afterwards. Word's format is binary on the outside because zipping is binary, even though there is text buried in the middle of it.8Text vs binary, side by side
| Text file | Binary file | |
|---|---|---|
| What the bytes mean | Character codes | Anything the format decides |
| Open in Notepad | Readable | Nonsense |
| Organised as | Lines, ending in \n | No lines at all |
| Who can read it | Any text editor | Only a program that knows the format |
| Size | Larger for numbers | Smaller and exact |
| Examples | .txt .py .csv .html | .png .pdf .docx .mp3 .exe |
9Recap
It lives on a disk and survives the program that made it. RAM does not — everything in it goes when the program ends.
Numbers from 0 to 255. What differs between files is what those numbers are meant to stand for.
Readable in Notepad, made of lines ending in \n. .txt, .py, .csv, .html.
Pixels, sound, compressed data, machine code. Readable only by a program that knows the format.
Comma Separated Values — one record per line, values split by commas. Not a third kind of file.
Word documents, PDFs, images, songs, videos, spreadsheets, installed programs. Text files are the exception.
- 1
Right-click a photo on your computer, choose Open with → Notepad, and look at what comes up.
Hint · Close it without saving. Notepad would rewrite the bytes it could not read and break the picture.
- 2
Now open one of your own
.pyfiles in Notepad. Why does that one read perfectly?Hint · A Python program is a text file. That is why IDLE can edit it.
- 3
Type three rows of names and marks into Notepad, save it as
marks.csv, and open it in Excel.Hint · Excel draws the grid. The file itself is still the lines you typed.
- 4
Go through the folders on your computer and count ten files. How many are text?
Hint · Judge by the extension, not by whether it looks like writing.
Why does a program need files at all?
What is actually different about a binary file?
A CSV file is...
Which of these four is a text file?