A text file stores data as human-readable characters encoded using ASCII or Unicode, so you can open it in any text editor. A binary file stores raw bytes that only make sense when interpreted by a specific program — used for images, audio, video, and compiled executables.
What is a text file?
A text file stores its contents as a sequence of characters, each represented using a character encoding such as ASCII or UTF-8. Every character — including numbers and punctuation — is stored as its character code, not as a raw numeric value.
For example, the number 42 in a text file is stored as two characters: '4' (ASCII 52) and '2' (ASCII 50) — two bytes total. You can open the file in Notepad or any text editor and read it directly.
Common text file types: .txt, .csv, .py, .html, .json, .md
A Python program writing to a text file:
with open("scores.txt", "w") as f:
f.write("Alice,95\n")
f.write("Bob,82\n")
f.write("Cara,78\n")
Opening scores.txt in a text editor shows exactly what was written — human-readable, comma-separated data.
What is a binary file?
A binary file stores raw bytes that represent data in a format specific to the application that created it. The bytes do not correspond to printable characters — they represent numeric values, pixel colours, audio samples, machine-code instructions, or compressed data.
For example, the integer 42 in a binary file might be stored as a single byte: 00101010 (the binary representation of 42) — just one byte, not two. This is more space-efficient for numbers.
Common binary file types: .jpg, .png, .mp3, .mp4, .exe, .pdf, .docx, .zip
A Python program reading a binary file:
with open("image.png", "rb") as f: # "rb" = read binary
header = f.read(8)
print(header) # b'\x89PNG\r\n\x1a\n' — PNG magic bytes
The first 8 bytes of every PNG file are always 89 50 4E 47 0D 0A 1A 0A (the PNG signature). No text editor can make sense of these bytes without knowing the PNG format specification.
What are the key differences between text and binary files?
| Feature | Text file | Binary file |
|---|---|---|
| Content | Human-readable characters | Raw bytes (machine-readable) |
| Openable in a text editor? | Yes — makes sense | Yes, but appears as garbled characters |
| Numbers stored as | Character codes (e.g. '4', '2' for 42) |
Raw binary value (e.g. 00101010 for 42) |
| Newline handling | Platform-specific (\n on Linux, \r\n on Windows) |
No automatic translation |
| Typical size | Larger for numeric data; compact for text | Smaller for numeric/multimedia data |
| Examples | .txt, .csv, .py, .html |
.jpg, .exe, .mp3, .png |
How do you open text and binary files in Python?
The mode string in open() determines how Python handles the file:
# Text mode (default) — reads/writes strings
with open("data.txt", "r") as f:
content = f.read() # returns a string
with open("data.txt", "w") as f:
f.write("Hello, world!\n") # writes a string
# Binary mode — reads/writes bytes objects
with open("image.jpg", "rb") as f:
data = f.read() # returns bytes
with open("output.bin", "wb") as f:
f.write(bytes([72, 101, 108, 108, 111])) # writes 5 bytes: "Hello"
The "b" flag switches between text and binary mode. Mixing modes causes errors: trying to write a string to a file opened in binary mode raises a TypeError.
Why does the distinction matter for GCSE programming projects?
Most GCSE projects use text files (typically .txt or .csv) to store and retrieve data, because:
- They are easy to inspect and debug (open in Notepad).
- Reading and writing strings requires no special parsing.
- CSV files integrate naturally with Python's
csvmodule.
Binary files become relevant if your project saves images, reads pre-existing data files in a proprietary format, or handles large numeric datasets where space efficiency matters. Understanding why a file is opened with "rb" rather than "r" — and what happens when you get this wrong — demonstrates deeper understanding that examiners reward.
What happens if you open a binary file in text mode by mistake?
Opening a binary file (e.g. a JPEG image) in text mode causes either garbled output, a UnicodeDecodeError, or silent data corruption depending on the content. Some byte sequences are valid UTF-8; others are not. In text mode on Windows, Python also translates \r\n byte pairs to \n, which corrupts any binary file containing those two bytes in sequence (a common occurrence). Always use "rb" for binary files.
Frequently asked questions
Is a .docx file a binary file?
Yes. Despite containing text, a .docx file is a binary file — specifically a ZIP archive containing XML files and embedded resources (images, fonts, styles). You cannot open it in a plain text editor and read it. Opening it in Python requires open("doc.docx", "rb"). The same is true for .xlsx and .pptx — all modern Office formats are binary ZIP containers.
Can a CSV file be considered binary?
No. A CSV (Comma-Separated Values) file is a plain text file. Every value is stored as a character string, and the file can be opened in any text editor. This is one reason CSV is widely used for data exchange — it requires no special software to inspect. By contrast, Excel's .xlsx format for the same data is binary.
How are images stored differently from text?
In an image file, bytes represent pixel colours (e.g. a 24-bit bitmap stores each pixel as three bytes: one each for red, green, and blue channel values from 0–255). There is no character encoding — the bytes are raw numeric values. In a text file, bytes represent characters. The value 72 in text means the character 'H' (ASCII 72); in an image, 72 might mean a specific shade of blue in a pixel. Same byte, completely different meaning — which is why the interpretation depends on the file format, not the bytes themselves.
Does file extension determine whether a file is text or binary?
By convention, but not by rule. A file's extension is just part of its name — the actual content determines whether it is text or binary. You could rename image.jpg to image.txt, but it would still be a binary file. Python's open() does not care about the extension; it only cares about the mode string ("r" or "rb"). The programmer must know which mode is appropriate for the data being handled.
Master file handling, data types, and every GCSE programming concept with tailored coaching from Professor Turing at aitutors.me.