KAHIBARO Bookshelves
Discord Login Register

10.5. Simple file-based programs

Why Build File-Based Programs?

By now you know how to open, read, and write files. In this chapter, you’ll use those skills to create small, practical programs that:

  • Remember information between runs (using a file as storage)
  • Read data from one file and write results to another
  • Process text to extract or transform information

You’ll see complete mini-programs that are still simple enough for beginners to read and modify.


General Pattern for File-Based Programs

Most simple file-based programs follow this pattern:

  1. Get input
    • From the user (input())
    • From a file (using open() and .read() / .readline() / looping)
  2. Process the data
    • Do calculations, filtering, counting, or formatting
  3. Save or display the result
    • Print to the screen
    • Write to a file with write() or writelines()

You can think of it like a factory:

$$
\text{File(s) + User Input} \;\rightarrow\; \text{Your Code} \;\rightarrow\; \text{Output File(s) + Screen Output}
$$


Example 1: A Simple Note Saver

This program lets the user type a note, and then saves it to a text file. Each note is added on a new line.

# simple_note_saver.py
filename = "notes.txt"
note = input("Type your note: ")
# Add the note to the file, keeping old notes
with open(filename, "a", encoding="utf-8") as file:
    file.write(note + "\n")
print("Note saved to", filename)

Key ideas specific to this style of program:

  • Use append mode "a" when you want to keep existing data and add more.
  • Add "\n" yourself if you want each note on its own line.
  • Only the file name is hard-coded; everything else comes from the user.

Try extending it:

  • Ask for a title and the note, and save both.
  • Add the current date/time to each note (using the datetime module, covered later).

Example 2: Viewing Saved Notes

Now add a second program to read and show the notes you saved.

# view_notes.py
filename = "notes.txt"
try:
    with open(filename, "r", encoding="utf-8") as file:
        contents = file.read()
    if contents.strip() == "":
        print("No notes found.")
    else:
        print("Your notes:")
        print(contents)
except FileNotFoundError:
    print("No notes file found yet. Add a note first.")

New pattern here:

  • Wrap open() in try / except to handle missing files without crashing.
  • Use .strip() to check if a file is effectively empty (only whitespace/newlines).

Example 3: A Very Simple Log Counter

Imagine you have a file log.txt where every line is some kind of event:

# Example log.txt
login
view_page
logout
login
view_page
view_page
logout

You can write a program that counts how many times each event appears.

# count_events.py
filename = "log.txt"
counts = {}
with open(filename, "r", encoding="utf-8") as file:
    for line in file:
        event = line.strip()
        if event == "":
            continue  # skip empty lines
        if event in counts:
            counts[event] += 1
        else:
            counts[event] = 1
print("Event counts:")
for event, count in counts.items():
    print(event, "=>", count)

What’s specific to this style of program:

  • It reads line by line and processes each line.
  • It uses a dictionary to accumulate results.
  • It ignores empty lines, which is common when processing text files.

You can adapt this pattern to:

  • Count how many times each word appears in a text file.
  • Count how many lines contain a certain word.

Example 4: Copying and Transforming a File

This program reads one file and writes a modified version to a new file. For example, you can convert all text to uppercase.

# shout_copy.py
source = "input.txt"
target = "output_shout.txt"
with open(source, "r", encoding="utf-8") as in_file, \
     open(target, "w", encoding="utf-8") as out_file:
    for line in in_file:
        upper_line = line.upper()
        out_file.write(upper_line)
print("Created", target)

Specific techniques here:

  • Open two files at the same time using a single with statement.
  • Transform each line before writing it out.
  • This pattern is common for text processing: read → change → write.

You could change:

  • line.upper() to line.lower()
  • or strip spaces: line.strip()
  • or replace text: line.replace("old", "new")

Example 5: Simple To-Do List (File-Based)

This example combines input, output, and file storage into one simple program.

It will:

  1. Load tasks from a file (if it exists)
  2. Show a menu to the user
  3. Let the user:
    • Add a task
    • View tasks
    • Save and quit
# todo_list.py
FILENAME = "tasks.txt"
def load_tasks():
    tasks = []
    try:
        with open(FILENAME, "r", encoding="utf-8") as file:
            for line in file:
                task = line.strip()
                if task:
                    tasks.append(task)
    except FileNotFoundError:
        # No file yet, start with empty list
        pass
    return tasks
def save_tasks(tasks):
    with open(FILENAME, "w", encoding="utf-8") as file:
        for task in tasks:
            file.write(task + "\n")
def show_tasks(tasks):
    if not tasks:
        print("No tasks yet.")
    else:
        print("Your tasks:")
        for i, task in enumerate(tasks, start=1):
            print(f"{i}. {task}")
def main():
    tasks = load_tasks()
    while True:
        print("\nMenu:")
        print("1. View tasks")
        print("2. Add task")
        print("3. Save and quit")
        choice = input("Choose an option (1-3): ")
        if choice == "1":
            show_tasks(tasks)
        elif choice == "2":
            new_task = input("Enter a new task: ")
            if new_task.strip():
                tasks.append(new_task.strip())
                print("Task added.")
            else:
                print("Empty task not added.")
        elif choice == "3":
            save_tasks(tasks)
            print("Tasks saved. Goodbye!")
            break
        else:
            print("Invalid choice. Please try again.")
if __name__ == "__main__":
    main()

What’s important about this example:

  • The file acts as a simple database of tasks.
  • Data is loaded once at the start and saved once at the end.
  • Tasks are stored one per line, a very common format for simple programs.
  • The program is interactive but still uses files for long-term storage.

Ideas to extend:

  • Allow deleting tasks.
  • Mark tasks as done and save this information.
  • Save both task text and priority by separating them with a character (for example, task|high).

Example 6: Searching in a File

This program asks the user for a word and then shows all lines from a file that contain that word.

# search_in_file.py
filename = "data.txt"
word = input("Enter a word to search for: ").strip()
if not word:
    print("You did not enter a word.")
else:
    try:
        with open(filename, "r", encoding="utf-8") as file:
            found_any = False
            for line_number, line in enumerate(file, start=1):
                if word in line:
                    print(f"Line {line_number}: {line.rstrip()}")
                    found_any = True
        if not found_any:
            print("No matches found.")
    except FileNotFoundError:
        print("File", filename, "not found.")

This shows a very common file-based pattern:

  • Get search criteria from the user.
  • Go through a file line by line, checking for matches.
  • Show matching lines with line numbers.

Combining Ideas: Design Tips for Simple File Programs

When designing your own file-based programs:

  1. Decide what the file represents
    • One item per line (tasks, notes, events)
    • One line with many values
    • Multiple files for different categories
  2. Choose how data is updated
    • Append new data ("a")
    • Rewrite the whole file ("w") after changes
    • Read-result → modify in memory → save once at the end
  3. Handle missing files gracefully
    • Use try / except FileNotFoundError
    • Start from an empty list or empty string if no file exists
  4. Keep the format simple
    • Start with “one thing per line”
    • Use simple separators like | or , only when needed
  5. Separate logic into functions
    • One function to load data from the file
    • One function to save data
    • Other functions to process the data

Practice Ideas

Here are small projects you can try to practice building file-based programs:

  • A daily journal that:
    • Adds a date and time to each entry
    • Lets you view all entries
  • A score tracker for a game:
    • Ask for player name and score
    • Save them to a file
    • Another program reads the file and shows the top scores
  • A contact list:
    • Save each contact’s name and email on one line
    • Let the user add contacts and list all contacts

In each case, think about:

  • What does one line represent?
  • How will the program load, modify, and save that data?

These simple patterns are the foundation for more advanced programs that work with larger data files, configuration files, or even simple databases.

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