Open a new coding project and you'll see files like app.py, data.csv, config.json, README.md, .env and requirements.txt. The ending after the dot, called the file extension, tells you and the computer what kind of file it is and which program should open it. This lesson explains file types and extensions, especially the ones programmers use every day.
There's one big split to understand first: some files are text, which you can open and read in any editor, and some are binary, which only make sense to a particular program. Knowing which is which tells you how to open, edit, compare and store a file, and why you should never edit an .xlsx file in Notepad.
What you need first: files, folders and file paths and how text, images and sound are stored.
Key terms
- File extension: the letters after the last dot in a file name:
.pyinapp.py. - File format: the rules for how data is arranged inside a type of file.
- Text file: a file that holds only characters, readable in any text editor.
- Binary file: a file that holds bytes meant for a program, not for reading as text. Images, Excel files and apps are binary.
- Plain text: text with no formatting such as bold or fonts. Code is always plain text.
- MIME type: a label like
text/htmlorimage/pngthat tells browsers and servers what a file is.
What a file extension does
The extension is just part of the name. It doesn't change what's inside the file. Renaming photo.jpg to photo.txt doesn't turn the photo into text; the bytes stay the same, and Notepad will show gibberish.
The extension does two useful jobs:
- It tells the operating system which app to open the file with when you double-click it.
- It tells you and other programs what format to expect.
Show extensions on your computer
Windows hides extensions by default, so report.pdf.exe (a dangerous program pretending to be a PDF) shows as report.pdf. Turn them on:
- Windows 11: File Explorer, View, Show, File name extensions.
- Windows 10: File Explorer, View tab, tick "File name extensions".
- macOS: Finder, Settings, Advanced, tick "Show all filename extensions".
Every programmer should have this on.
Text files and binary files
| Text files | Binary files | |
|---|---|---|
| Contains | Characters, usually UTF-8 | Bytes in a program-specific format |
| Open in a text editor? | Yes, readable | Shows gibberish |
| Examples | .txt, .py, .csv, .json, .html, .md | .jpg, .png, .pdf, .xlsx, .docx, .zip, .exe |
| Git can show changes line by line? | Yes | No |
You can see the difference by looking at the first bytes of each kind. Text files start with readable characters; many binary formats start with a "magic number" that identifies the format:
text_bytes = "name,marks\nAhana,88\n".encode("utf-8")
png_start = bytes([137, 80, 78, 71, 13, 10, 26, 10]) # every PNG file starts with these 8 bytes
print(text_bytes[:10])
print(png_start)
b'name,marks'
b'\x89PNG\r\n\x1a\n'
The text is just its letters. The PNG header is mostly unreadable bytes, with "PNG" in the middle so people can spot it. Programs check these first bytes, not the extension, to know what a file really is.
File types programmers use every day
Code files
| Extension | Language | Notes |
|---|---|---|
.py | Python | Python scripts and modules |
.ipynb | Jupyter notebook | Code, output and notes together; actually JSON inside |
.js | JavaScript | Runs in browsers and Node.js |
.ts | TypeScript | JavaScript with types |
.html | HTML | The structure of web pages |
.css | CSS | The styling of web pages |
.java, .c, .cpp | Java, C, C++ | Compiled languages |
.sql | SQL | Database queries |
.sh | Shell script | Terminal commands saved in a file (Mac and Linux) |
.ps1, .bat | PowerShell, batch | Windows scripts |
All code files are plain text. You can open any of them in VS Code or Notepad.
Data files
| Extension | What it holds | Example content |
|---|---|---|
.txt | Plain text, no structure | Remember to revise loops |
.csv | A table, one row per line, values separated by commas | name,marks then Ahana,88 |
.tsv | Like CSV, separated by tabs | |
.json | Structured data: objects and lists | {"name": "Ahana", "marks": 88} |
.xml | Structured data with tags | <student><name>Ahana</name></student> |
.xlsx | An Excel workbook (binary: zipped XML) | Several sheets, formulas, formatting |
.parquet | A compressed table format for big data | Used with pandas and Spark |
.db, .sqlite | A SQLite database file | Tables, binary |
CSV and JSON are the two you'll use most. A CSV file is just text, which is why it opens in both Excel and Notepad. Here's one written and read back with Python's built-in csv module:
import csv
with open("marks.csv", "w", newline="", encoding="utf-8") as f:
writer = csv.writer(f)
writer.writerow(["name", "marks"])
writer.writerow(["Ahana", 88])
writer.writerow(["Rohan", 72])
with open("marks.csv", encoding="utf-8") as f:
print(f.read())
name,marks
Ahana,88
Rohan,72
JSON for beginners covers JSON in detail, and Python file handling shows how to work with both.
Settings and project files
| File | Purpose |
|---|---|
.env | Secret settings like API keys and passwords. Never share it or upload it to GitHub |
.yaml or .yml | Settings in a readable format, used by Docker, GitHub Actions and Kubernetes |
.toml | Settings, used by Python projects in pyproject.toml |
.ini, .cfg | Older settings formats |
requirements.txt | The Python packages a project needs |
package.json | A JavaScript project's settings and packages |
.gitignore | Files Git should not track |
Dockerfile | Instructions to build a Docker container; no extension at all |
Documentation
| Extension | Holds |
|---|---|
.md | Markdown: plain text with simple symbols for headings, bold and lists. Every GitHub project has a README.md |
.pdf | Fixed-layout documents; binary |
.docx | Word documents; binary |
Images, audio and video
| Extension | Type | Compression |
|---|---|---|
.png | Image | Lossless; best for screenshots, logos and charts |
.jpg / .jpeg | Image | Lossy; best for photos |
.webp | Image | Both; smaller files for websites |
.svg | Image | Vector graphics, stored as text (XML); sharp at any size |
.gif | Image | Short animations, few colours |
.mp3, .aac | Audio | Lossy |
.wav | Audio | Usually uncompressed |
.mp4, .mkv | Video | Lossy |
Archives and programs
| Extension | What it is |
|---|---|
.zip | Compressed archive of files and folders |
.tar.gz | A Linux archive: files bundled with tar, then compressed with gzip |
.exe, .msi | Windows programs and installers |
.dmg, .app | macOS disk images and apps |
.apk | Android apps |
.deb, .rpm | Linux software packages |
A file can have more than one dot: backup.tar.gz is a tar archive that was then gzipped. The last extension is the outer layer.
Safety with file types
- Never run an
.exe,.msi,.bat,.ps1,.apkor.shfile you don't trust. They can do anything your user account can. - Watch for double extensions like
invoice.pdf.exe. With extensions shown, you'll spot them. - Office files can contain macros (
.xlsm,.docm); don't enable macros from unknown senders. - Text and data files (
.txt,.csv,.json,.md) are safe to open in a text editor.
Common mistakes
- Renaming a file's extension to "convert" it. Use a program that actually converts it, such as "Save as CSV" in Excel.
- Opening a large CSV in Excel and saving it. Excel may change values, for example turning long ID numbers into
1.23E+15or removing leading zeros from PIN codes and phone numbers. Read CSVs with Python or pandas for data work. - Editing code in Word. It adds formatting and curly quotes that break code. Use a code editor.
- Uploading
.envfiles to GitHub, which leaks passwords and API keys.
Practice
- Which of these are text files:
notes.md,logo.png,app.js,report.pdf,data.json,config.yaml?
Show the answer
Text: notes.md, app.js, data.json and config.yaml. Binary: logo.png and report.pdf.
- Which file type would you choose for each: a company logo for a website, a photo from a trip, a table of 10,000 sales, settings for a GitHub Actions workflow?
Show the answer
Logo: .svg (sharp at any size) or .png. Trip photo: .jpg or .webp. Sales table: .csv (or .parquet for very large data). GitHub Actions settings: .yml, because that's the format it reads.
- A friend sends
holiday_photos.zip.exe. What should you do?
Show the answer
Don't open it. It's a program (.exe) disguised as a zip file, a common trick for spreading malware. Delete it and tell your friend their account may be compromised.
- Write a Python program that creates
cities.csvwith a header rowcity,stateand two rows, then prints how many lines it has.
Show the answer
import csv
with open("cities.csv", "w", newline="", encoding="utf-8") as f:
writer = csv.writer(f)
writer.writerow(["city", "state"])
writer.writerow(["Pune", "Maharashtra"])
writer.writerow(["Kochi", "Kerala"])
with open("cities.csv", encoding="utf-8") as f:
print(len(f.readlines()))
3
- Why does Git show line-by-line changes for
app.pybut not forreport.docx?
Show the answer
app.py is a text file, so Git can compare it line by line. A .docx file is binary (zipped XML), so Git can only tell that some bytes changed, not what changed in a readable way.
Key takeaways
- The extension is part of the name; it hints at the format but doesn't change the contents.
- Text files (code, CSV, JSON, Markdown, YAML) open in any editor; binary files need their own program.
- Programmers mostly use
.py,.csv,.json,.md,.yaml,.txtand.envfiles. - Turn on file extensions in your file manager, and never run programs you don't trust.
- Keep secrets in
.envfiles and never upload them.
Frequently asked questions
What is the difference between CSV and Excel files?
A CSV file is plain text holding one table, with values separated by commas, and no formatting or formulas. An .xlsx file is a binary Excel workbook that can hold several sheets, formulas, charts and formatting. CSV is simpler and works with almost every program.
Can a file have no extension?
Yes. Many important files have none, like Dockerfile, Makefile and LICENSE, and on Linux many programs have no extension. The OS or the tool works out what they are from their name or contents.
How do I open a JSON or CSV file?
Any text editor works, and VS Code shows them neatly. Excel opens CSV files as tables. Browsers like Firefox show JSON in a readable layout. In code, Python's csv and json modules read them.
What to learn next
- Meet the programmer's main tool in what is the terminal (command line)?
- Learn the most common data format in JSON for beginners.
- Read and write these files in code with Python file handling.