KAHIBARO Bookshelves
Discord Login Register

10.1. 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:

  • Data is saved on disk, not just in memory.
  • The data stays there until you delete or change the file.
  • You (or other programs) can use the data again later.

Examples of files you already use:

  • A .txt document with notes.
  • An .mp3 music file.
  • A .jpg photo.
  • A .py Python script.

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

File Names and Extensions

Every file has:

  • A name, like shopping_list
  • Often an extension, like .txt or .csv

Together they form the file name, like:

  • shopping_list.txt
  • data.csv
  • script.py

The extension:

  • Is usually the part after the last dot in the file name.
  • Gives humans and programs a hint about the file’s format or type.
  • Does not actually store the type; it’s more of a convention.

Some common extensions you will likely meet in Python work:

  • .txt – plain text
  • .csv – comma-separated values (simple table-like data)
  • .json – structured data, often used for configuration or web APIs
  • .py – Python source code files

Where Files Live: Folders and Paths

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

  • Files
  • Other folders

You can think of it as a tree:

  • The top-level is often called the root.
  • Each folder can have subfolders, and so on.

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:

  • Absolute path: Describes the location from the root of the file system.
    Example (Windows): C:\Users\Alice\Documents\notes.txt
    Example (macOS/Linux): /home/alice/Documents/notes.txt
  • Relative path: Describes the location starting from “where you are now” (the current folder).
    Example: notes.txt (the file is in the current folder)
    Example: data/values.csv (inside a data subfolder)

You will use these paths in Python when opening files.

How Computers See Files: Bytes

Inside, a file is just a sequence of bytes.

  • A byte is a small unit of data (8 bits).
  • Every type of file—text, image, audio—is stored as bytes.
  • The meaning of those bytes depends on the file format.

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

  • An ordered sequence:
    $$ b_1, b_2, b_3, \dots, b_n $$
  • Where each $b_i$ is a byte.

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:

  • .txt – plain text
  • .py – Python code
  • .csv – table-like data, where lines and commas separate values
  • .json – structured text data

Conceptually:

  • The file is a sequence of characters.
  • An encoding (such as UTF-8) turns each character into bytes and back.

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:

  • Pixel data in an image (.png, .jpg)
  • Sound samples in an audio file (.mp3, .wav)
  • Compiled programs (.exe)
  • Some specialized data formats

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:

  • Reads and writes raw bytes directly.
  • Does not try to interpret them as characters.

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:

  • Read its contents (e.g., to process saved data).
  • Write new data (e.g., to log results, save a configuration, store user input).

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:

  • A file is an external, named storage of data that your program can use over multiple runs.

Files and Data Organization

Files are one of the simplest ways to organize data:

  • You can use one file per type of data, such as:
    • users.txt for user names
    • settings.json for configuration
  • You can use folders to group related files:
    • data/2024/sales.csv
    • data/2024/customers.csv

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:

  • Loading input data from a text or CSV file.
  • Saving the results of a calculation.
  • Keeping logs of what your program did.
  • Reading configuration from JSON files.
  • Storing small amounts of data without needing a full database.

All of those tasks rely on the basic concept:

  • A file is a named, persistent sequence of bytes on disk that your program can read from and write to.

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: 0

Comments

Please login to add a comment.

Don't have an account? Register now!

Where to Move

Move chapter:

  • ☰ 1. Chapter 1: What Is Programming?
    • ☰ 1.1. What is a computer program?
      • ☰ 1.2. What is Python?
        • ☰ 1.3. Where is Python used?
          • ☰ 1.4. How programming helps solve problems
            • ☰ 1.5. Your first Python program
            • ☰ 2. Chapter 2: Setting Up Python
              • ☰ 2.1. Installing Python (Windows / macOS / Linux)
                • ☰ 2.2. Using Python in the terminal
                  • ☰ 2.3. Using an IDE (IDLE / VS Code)
                    • ☰ 2.4. Running Python scripts
                      • ☰ 2.5. Python interactive mode
                      • ☰ 3. Chapter 3: Variables and Data Types
                        • ☰ 3.1. What is a variable?
                          • ☰ 3.2. Creating variables
                            • ☰ 3.3. Numbers
                              • ☰ 3.4. Test
                                • ☰ 3.5. Boolean values
                                  • ☰ 3.6. Type checking
                                  • ☰ 4. Chapter 4: Basic Operations
                                    • ☰ 4.1. Arithmetic operations
                                      • ☰ 4.2. Working with numbers
                                        • ☰ 4.3. String operations
                                          • ☰ 4.4. Combining text and variables
                                            • ☰ 4.5. Comments in Python
                                            • ☰ 5. Chapter 5: User Input and Output
                                              • ☰ 5.1. Getting input from the user
                                                • ☰ 5.2. Converting input to numbers
                                                  • ☰ 5.3. Formatting output
                                                    • ☰ 5.4. Writing interactive programs
                                                    • ☰ 6. Chapter 6: Conditions
                                                      • ☰ 6.1. Comparison operators
                                                        • ☰ 6.2. Logical operators
                                                          • ☰ 6.3. if, elif, else
                                                            • ☰ 6.4. Nested conditions
                                                              • ☰ 6.5. Common beginner mistakes
                                                              • ☰ 7. Chapter 7: Loops
                                                                • ☰ 7.1. Why loops are useful
                                                                  • ☰ 7.2. for loops
                                                                    • ☰ 7.3. while loops
                                                                      • ☰ 7.4. Loop counters
                                                                        • ☰ 7.5. break and continue
                                                                          • ☰ 7.6. Loop exercises
                                                                          • ☰ 8. Chapter 8: Data Collections
                                                                            • ☰ 8.1. Lists
                                                                              • ☰ 8.2. Tuples
                                                                                • ☰ 8.3. Dictionaries
                                                                                  • ☰ 8.4. Sets (basics)
                                                                                    • ☰ 8.5. Choosing the right data structure
                                                                                    • ☰ 9. Chapter 9: Functions
                                                                                      • ☰ 9.1. Why functions matter
                                                                                        • ☰ 9.2. Defining functions
                                                                                          • ☰ 9.3. Parameters and arguments
                                                                                            • ☰ 9.4. Return values
                                                                                              • ☰ 9.5. Scope (local vs global)
                                                                                                • ☰ 9.6. Reusing code
                                                                                                • ☰ 10. Chapter 10: Working with Files
                                                                                                  • ☰ 10.1. What is a file?
                                                                                                    • ☰ 10.2. Reading text files
                                                                                                      • ☰ 10.3. Writing and appending files
                                                                                                        • ☰ 10.4. File paths
                                                                                                          • ☰ 10.5. Simple file-based programs
                                                                                                          • ☰ 11. Chapter 11: Errors and Debugging
                                                                                                            • ☰ 11.1. Types of errors
                                                                                                              • ☰ 11.2. Reading error messages
                                                                                                                • ☰ 11.3. Common beginner errors
                                                                                                                  • ☰ 11.4. try and except
                                                                                                                    • ☰ 11.5. Debugging strategies
                                                                                                                    • ☰ 12. Chapter 12: Using Python Libraries
                                                                                                                      • ☰ 12.1. What is a library?
                                                                                                                        • ☰ 12.2. Importing modules
                                                                                                                          • ☰ 12.3. Standard libraries
                                                                                                                            • ☰ 12.3.1. math
                                                                                                                              • ☰ 12.3.2. random
                                                                                                                                • ☰ 12.3.3. datetime
                                                                                                                                • ☰ 12.4. Installing external libraries with pip
                                                                                                                                • ☰ 13. Chapter 13: Object-Oriented Programming
                                                                                                                                  • ☰ 13.1. What is object-oriented programming?
                                                                                                                                    • ☰ 13.2. Classes and objects
                                                                                                                                      • ☰ 13.3. The `__init__` method
                                                                                                                                        • ☰ 13.4. Attributes and methods
                                                                                                                                          • ☰ 13.5. Encapsulation
                                                                                                                                            • ☰ 13.6. Inheritance (basics)
                                                                                                                                              • ☰ 13.7. Simple OOP examples
                                                                                                                                              • ☰ 14. Chapter 14: Automation with Python
                                                                                                                                                • ☰ 14.1. What is automation?
                                                                                                                                                  • ☰ 14.2. Automating simple tasks
                                                                                                                                                    • ☰ 14.3. Working with files and folders
                                                                                                                                                      • ☰ 14.4. Automating text processing
                                                                                                                                                        • ☰ 14.5. Scheduling scripts
                                                                                                                                                          • ☰ 14.6. Practical automation examples
                                                                                                                                                          • ☰ 15. Chapter 15: Web Development with Python
                                                                                                                                                            • ☰ 15.1. How the web works (basic overview)
                                                                                                                                                              • ☰ 15.2. Introduction to Flask
                                                                                                                                                                • ☰ 15.3. Creating a simple web server
                                                                                                                                                                  • ☰ 15.4. Routing and templates
                                                                                                                                                                    • ☰ 15.5. Handling user input (forms)
                                                                                                                                                                      • ☰ 15.6. Very basic APIs
                                                                                                                                                                      • ☰ 16. Chapter 16: Data Science with Python
                                                                                                                                                                        • ☰ 16.1. What is data science?
                                                                                                                                                                          • ☰ 16.2. Working with data
                                                                                                                                                                            • ☰ 16.3. Introduction to NumPy
                                                                                                                                                                              • ☰ 16.4. Introduction to pandas
                                                                                                                                                                                • ☰ 16.5. Data visualization with matplotlib
                                                                                                                                                                                  • ☰ 16.6. Simple data analysis examples
                                                                                                                                                                                  • ☰ 17. Chapter 17: Mini Projects
                                                                                                                                                                                    • ☰ 17.1. Number guessing game
                                                                                                                                                                                      • ☰ 17.2. Simple calculator
                                                                                                                                                                                        • ☰ 17.3. To-do list
                                                                                                                                                                                          • ☰ 17.4. Password generator
                                                                                                                                                                                            • ☰ 17.5. Quiz application
                                                                                                                                                                                              • ☰ 17.6. Small web or data project
                                                                                                                                                                                              • ☰ 18. Chapter 18: Next Steps
                                                                                                                                                                                                • ☰ 18.1. How to practice Python
                                                                                                                                                                                                  • ☰ 18.2. Writing clean and readable code
                                                                                                                                                                                                    • ☰ 18.3. Choosing a specialization
                                                                                                                                                                                                      • ☰ 18.3.1. Web development
                                                                                                                                                                                                        • ☰ 18.3.2. Data science
                                                                                                                                                                                                          • ☰ 18.3.3. Automation
                                                                                                                                                                                                            • ☰ 18.3.4. Software development
                                                                                                                                                                                                            • ☰ 18.4. Learning resources and communities
                                                                                                                                                                                                            • ☰ 19. Appendix
                                                                                                                                                                                                              • ☰ 19.1. Python syntax cheat sheet
                                                                                                                                                                                                                • ☰ 19.2. Common errors and solutions
                                                                                                                                                                                                                  • ☰ 19.3. Glossary of terms