LambdaLabTM
Computer Science · Class 12 · File Handling
FilesText vs binary⏱️ 14 min read

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.

RAM — while the program runs
  • Holds your variables and lists
  • Very fast
  • Emptied when the program ends
  • Emptied when the power goes
A file — on the disk
  • Holds whatever you wrote into it
  • Slower to reach
  • Still there after the program ends
  • Still there after a restart
A file, defined

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.

Key Takeaway
Storing data in a file is called persistent storage. “Persistent” just means it persists — it stays. RAM is the opposite: temporary, or volatile, storage.

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

Every station is the same kind of thing: radio waves in the air. What makes one music and another cricket commentary is how the receiver is meant to read them. Tune a music station in on the wrong setting and you get a screech — the signal was fine, you just read it the wrong way. Files are the same, and the screech is what a binary file looks like in Notepad.

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.

what_text_is.py

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.

A text file is
  • 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.
The test that always works
Open it in Notepad. If you can read it, it is a text file. If you get symbols and boxes, it is binary. That one experiment settles it faster than any list of extensions.

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.

A binary file is
  • 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.

Open each of these in Notepad and look
Text fileA note you typed in Notepad
What is really on the disk — the first bytes, in hex
48656C6C6F0A4D656574206174203520
What a text editor shows you
HelloMeetat5
= newline = space = a byte that is not printable text

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.

Watch Out
A binary file is not “written in binary” and a text file is not “written in letters”. Both are stored as binary numbers — that is the only thing a disk can do. The difference is entirely in what the numbers are for.

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:

marks.csv
name,marks
Asha,92
Rohit,78
Meera,85

The 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.

Key Takeaway
A CSV file is a text file. It is not a third kind of file sitting between text and binary. It is an ordinary text file that follows one extra rule — put a comma between the values — so that any program can work out where one value ends and the next begins.

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:

Every filesplits intotextorbinary
textincludes.txt.py.csv

CSV gets a name of its own because of what people do with it, not because of how it is stored.

Careful: .csv and .xlsx are not the same
Both open in Excel and both look like a grid, so students mix them up constantly. A .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.

.txt
A plain note
text
.py
A Python program
text
.csv
A table of data
text
.html
A web page
text
.docx
A Word document
binary
.pdf
A question paper
binary
.jpg
A photo
binary
.png
A screenshot
binary
.mp3
A song
binary
.mp4
A video
binary
.xlsx
An Excel workbook
binary
.exe
An installed app
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.

A .docx really is a zip folder
Take any .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

Let's Recap!
Text fileBinary file
What the bytes meanCharacter codesAnything the format decides
Open in NotepadReadableNonsense
Organised asLines, ending in \nNo lines at all
Who can read itAny text editorOnly a program that knows the format
SizeLarger for numbersSmaller and exact
Examples.txt .py .csv .html.png .pdf .docx .mp3 .exe
Why binary is smaller for numbers
Store the number 1000000 in a text file and it takes 7 bytes, one per digit. Store it in a binary file and it takes 4, because the bytes hold the number itself rather than a picture of it. Multiply that by a million rows and the difference stops being academic — which is why photos, videos and databases are all binary.

9Recap

A file is named, permanent data

It lives on a disk and survives the program that made it. RAM does not — everything in it goes when the program ends.

Every file is a row of bytes

Numbers from 0 to 255. What differs between files is what those numbers are meant to stand for.

Text file: the bytes are character codes

Readable in Notepad, made of lines ending in \n. .txt, .py, .csv, .html.

Binary file: the bytes mean something else

Pixels, sound, compressed data, machine code. Readable only by a program that knows the format.

CSV is a text file

Comma Separated Values — one record per line, values split by commas. Not a third kind of file.

Most files you meet are binary

Word documents, PDFs, images, songs, videos, spreadsheets, installed programs. Text files are the exception.

✍️ Now write these yourself
  1. 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. 2

    Now open one of your own .py files in Notepad. Why does that one read perfectly?

    Hint · A Python program is a text file. That is why IDLE can edit it.

  3. 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. 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.

Quick Check

Why does a program need files at all?

Quick Check

What is actually different about a binary file?

Quick Check

A CSV file is...

Quick Check

Which of these four is a text file?