Skip to content

Computer Science · Ch 2 — File Handling in Python

Types of Files

2.2

Types of Files

Files Are Binary at Heart

Every file on a computer — whether it is a text document, an image, a video, or a program — is stored as a collection of 0s and 1s. That is the only language a computer understands. So at the most basic level, every file is just a series of bytes (each byte being a group of 8 bits) placed one after the other. But how we interpret those bytes makes all the difference.

Two Main Types of Data Files

Based on how the bytes are meant to be read, data files fall into two broad categories:

  • Text files
  • Binary files
Text Files

A text file contains data that is human-readable. The bytes in a text file represent characters — letters, digits, punctuation, spaces, and newlines — using a standard encoding like ASCII or Unicode. Because of this, you can open a text file in any simple text editor (like Notepad, gedit, or VS Code) and read its contents directly.

Examples: .txt, .py, .html, .csv files.

Note

Even though a text file looks like plain English (or any language), it is still stored as 0s and 1s on the disk. The text editor simply translates those bytes back into characters for you.

Binary Files

A binary file contains data that is not meant to be read as text. The bytes in a binary file can represent anything — an image pixel, a sound sample, a machine instruction, a floating-point number, or a complex data structure. These bytes are not human-readable; if you open a binary file in a text editor, you will see gibberish — strange symbols, random characters, and unprintable codes.

Because of this, binary files require a specific program to access and interpret their contents. For example, an image file (.jpg, .png) needs an image viewer, a music file (.mp3) needs a media player, and an executable file (.exe) needs the operating system to run it.

Examples: .jpg, .png, .mp3, .exe, .dat, .pdf.

Watch out

A common mistake is to try to open a binary file in a text editor and expect to see meaningful content. You will only see garbage. Always use the correct application for the file type.

Key Difference at a Glance

FeatureText FileBinary File
ReadabilityHuman-readable charactersNon-human-readable bytes
Can be opened byAny text editorOnly specific programs
Typical contentsLetters, numbers, punctuationImages, audio, video, compiled code
Common extensions.txt, .py, .html, .csv.jpg, .mp3, .exe, .pdf

Why This Distinction Matters in Python …