Module 3 · Beginner · Files, folders and the terminal

File types and extensions explained

File types and extensions for programmers: text vs binary files, and what .txt, .csv, .json, .py, .html, .md, .yaml, .env, .zip and more contain.

By theprogrammers team · · 8 min read

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: .py in app.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/html or image/png that 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:

  1. It tells the operating system which app to open the file with when you double-click it.
  2. 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 filesBinary files
ContainsCharacters, usually UTF-8Bytes in a program-specific format
Open in a text editor?Yes, readableShows gibberish
Examples.txt, .py, .csv, .json, .html, .md.jpg, .png, .pdf, .xlsx, .docx, .zip, .exe
Git can show changes line by line?YesNo

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

ExtensionLanguageNotes
.pyPythonPython scripts and modules
.ipynbJupyter notebookCode, output and notes together; actually JSON inside
.jsJavaScriptRuns in browsers and Node.js
.tsTypeScriptJavaScript with types
.htmlHTMLThe structure of web pages
.cssCSSThe styling of web pages
.java, .c, .cppJava, C, C++Compiled languages
.sqlSQLDatabase queries
.shShell scriptTerminal commands saved in a file (Mac and Linux)
.ps1, .batPowerShell, batchWindows scripts

All code files are plain text. You can open any of them in VS Code or Notepad.

Data files

ExtensionWhat it holdsExample content
.txtPlain text, no structureRemember to revise loops
.csvA table, one row per line, values separated by commasname,marks then Ahana,88
.tsvLike CSV, separated by tabs
.jsonStructured data: objects and lists{"name": "Ahana", "marks": 88}
.xmlStructured data with tags<student><name>Ahana</name></student>
.xlsxAn Excel workbook (binary: zipped XML)Several sheets, formulas, formatting
.parquetA compressed table format for big dataUsed with pandas and Spark
.db, .sqliteA SQLite database fileTables, 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

FilePurpose
.envSecret settings like API keys and passwords. Never share it or upload it to GitHub
.yaml or .ymlSettings in a readable format, used by Docker, GitHub Actions and Kubernetes
.tomlSettings, used by Python projects in pyproject.toml
.ini, .cfgOlder settings formats
requirements.txtThe Python packages a project needs
package.jsonA JavaScript project's settings and packages
.gitignoreFiles Git should not track
DockerfileInstructions to build a Docker container; no extension at all

Documentation

ExtensionHolds
.mdMarkdown: plain text with simple symbols for headings, bold and lists. Every GitHub project has a README.md
.pdfFixed-layout documents; binary
.docxWord documents; binary

Images, audio and video

ExtensionTypeCompression
.pngImageLossless; best for screenshots, logos and charts
.jpg / .jpegImageLossy; best for photos
.webpImageBoth; smaller files for websites
.svgImageVector graphics, stored as text (XML); sharp at any size
.gifImageShort animations, few colours
.mp3, .aacAudioLossy
.wavAudioUsually uncompressed
.mp4, .mkvVideoLossy

Archives and programs

ExtensionWhat it is
.zipCompressed archive of files and folders
.tar.gzA Linux archive: files bundled with tar, then compressed with gzip
.exe, .msiWindows programs and installers
.dmg, .appmacOS disk images and apps
.apkAndroid apps
.deb, .rpmLinux 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, .apk or .sh file 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+15 or 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 .env files to GitHub, which leaks passwords and API keys.

Practice

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

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

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

  1. Write a Python program that creates cities.csv with a header row city,state and 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
  1. Why does Git show line-by-line changes for app.py but not for report.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, .txt and .env files.
  • Turn on file extensions in your file manager, and never run programs you don't trust.
  • Keep secrets in .env files 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

  1. Meet the programmer's main tool in what is the terminal (command line)?
  2. Learn the most common data format in JSON for beginners.
  3. Read and write these files in code with Python file handling.