RUNLOCALAIv38
->Will it run?Best GPUCompareTroubleshootStartLearnPulseModelsHardwareToolsBench
Run check
RUNLOCALAI

Independently operated catalog for local-AI hardware and software. Hand-written verdicts. Source-cited claims. Reproducible commands when we have them.

OP·Eruo Fredoline
DIR
  • Models
  • Hardware
  • Tools
  • Benchmarks
TOOLS
  • Will it run?
  • Compare hardware
  • Cost vs cloud
  • Choose my GPU
  • Prompting kits
  • Quick answers
REF
  • All buyer guides
  • Learn local AI
  • Methodology
  • Glossary
  • Errors KB
  • Trust
EDITOR
  • About
  • Author
  • How we make money
  • Editorial policy
  • Contact
LEGAL
  • Privacy
  • Terms
  • Sitemap
MAIL · MONTHLY DIGEST
Get monthly local AI changes
Monthly recap. No spam.
DISCLOSURE

Some links on this site are affiliate links (Amazon Associates and other first-class retailers). When you buy through them, we earn a small commission at no extra cost to you. Affiliate links do not influence our verdicts — there are cards we rate highly that we don't have affiliate relationships with, and cards that sell well that we refuse to recommend. Read more →

© 2026 runlocalai.coIndependently operated
RUNLOCALAI · v38
  1. >
  2. Home
  3. /Learn
  4. /Courses
  5. /Python for AI — Zero to Useful
  6. /Ch. 8
Python for AI — Zero to Useful

08. File I/O

Chapter 8 of 36 · 20 min
KEY INSIGHT

Always use `with open()` for file operations. `json.dump()` and `json.load()` handle the serialization between Python dictionaries and JSON text.

Reading Files

# Read entire file
with open("prompts.txt", "r") as f:
    content = f.read()
    print(content)

The with statement ensures the file closes even if errors occur.

Reading Lines

# Read as list of lines
with open("prompts.txt", "r") as f:
    lines = f.readlines()

# Or iterate line by line (memory efficient for large files)
with open("prompts.txt", "r") as f:
    for line in f:
        print(line.strip())

Writing Files

# Write (overwrites existing content)
with open("output.txt", "w") as f:
    f.write("Hello, AI\n")
    f.write("Second line\n")

# Append (adds to existing content)
with open("output.txt", "a") as f:
    f.write("Third line\n")

JSON Files

AI systems frequently use JSON for configuration and data exchange:

import json

config = {
    "model": "gpt-4",
    "temperature": 0.7,
    "batch_size": 32
}

# Write JSON
with open("config.json", "w") as f:
    json.dump(config, f, indent=2)

# Read JSON
with open("config.json", "r") as f:
    loaded_config = json.load(f)

Handling Missing Files

import json
from pathlib import Path

config_path = Path("config.json")

if config_path.exists():
    with open(config_path) as f:
        config = json.load(f)
else:
    config = {"model": "gpt-4", "temperature": 0.7}
    with open(config_path, "w") as f:
        json.dump(config, f, indent=2)

Local verification checkpoint

Run the smallest example from this chapter in a local workspace and record the package version, runtime, data path, and observed output. If the result depends on model size, vector count, CPU/GPU backend, or available memory, note that constraint beside the exercise so the lesson remains reproducible.

Local verification checkpoint

Run the smallest example from this chapter in a local workspace and record the package version, runtime, data path, and observed output. If the result depends on model size, vector count, CPU/GPU backend, or available memory, note that constraint beside the exercise so the lesson remains reproducible.

EXERCISE
  1. Create a JSON file with three AI model configurations
  2. Read it back
  3. Add a fourth model
  4. Write it back
← Chapter 7
Dictionaries for AI Config
Chapter 9 →
Error Handling