Kahibaro
Discord Login Register

What is a file?

Understanding Files

In programming, a file is a named collection of data stored on a device (like your hard drive, SSD, or USB stick). Python programs often read from files and write data to them, so understanding what a file is—and how it’s organized—is important before you start working with them in code.

This section focuses on what files are conceptually, not yet on the Python code to use them.

Files as Long-Term Storage

When your program runs, it uses memory (RAM), which is temporary. As soon as the program stops (or the computer is turned off), anything stored only in memory is lost.

A file provides persistent storage:

Examples of files you already use:

From Python’s point of view, all of these are just data stored in files.

File Names and Extensions

Every file has:

Together they form the file name, like:

The extension:

Some common extensions you will likely meet in Python work:

Where Files Live: Folders and Paths

Files are organized in folders (also called directories). Folders can contain:

You can think of it as a tree:

To uniquely describe where a file is, you use a path.

Absolute vs Relative Paths (Concept Only)

Without going into Python code yet, you will see two general ideas:

You will use these paths in Python when opening files.

How Computers See Files: Bytes

Inside, a file is just a sequence of bytes.

From a programming perspective, you can think of a file as:

Python can read or write these bytes directly or interpret them as text using an encoding (like UTF-8).

Text Files vs Binary Files

A very important distinction for programming is between text files and binary files.

Text Files

Text files store human-readable text. They contain characters like letters, digits, punctuation, and special characters (spaces, newlines, etc.), all represented using an encoding.

Common examples:

Conceptually:

When Python works with text files, it typically:

  1. Reads bytes from the file.
  2. Decodes them into characters using an encoding.

Binary Files

Binary files store data that is not meant to be read as plain text. The bytes represent other kinds of information, such as:

When a human opens a binary file in a text editor, it usually looks like random characters, because the bytes are not meant to map cleanly to readable text.

When Python works with binary files, it:

How Programs Use Files

Most interactions with a file follow the same general pattern:

  1. Open the file.
  2. Read from it or write to it.
  3. Close the file.

Conceptually, when a file is open, Python has a connection to that file on disk. You then:

The details of how to open, read, write, and close files are handled in later sections of this chapter. For now, the key idea is that:

Files and Data Organization

Files are one of the simplest ways to organize data:

Later, you might use more advanced storage (like databases), but they still rely on files underneath. Knowing what a file is and how it behaves is the foundation.

Why Files Matter in Python

In many practical Python programs, files are involved, for example:

All of those tasks rely on the basic concept:

In the next sections of this chapter, you will see how to use Python to read existing files, create new ones, and work with their contents.

Views: 16

Comments

Please login to add a comment.

Don't have an account? Register now!