1%
OnePercentDev
LearnRoadmapsCommunityPodcastConsult
Log inJoin OnePercentDev
1%
OnePercentDev

Build. Debug. Ship. AI-ready.
Disciplined daily learning for the AI era.

Navigation

  • Learn
  • Roadmaps
  • Community
  • Podcast
  • Consult

Platform

  • Python for AI
  • Create Account
  • Dashboard

Stay Updated

Get notified about new courses and content drops.

© 2026 OnePercentDev. All rights reserved.

Python for AI
Part 39
Lesson 41
12:24

Python Files in AI Era: How Python Reads PDF, Images, .env & Agent Memory | Part-39

Subscribe on YouTube

Up Next

41 / 58
  • 20:17

    Production-Level Debugging: Find Bugs Without Guessing | Python in Kannada | Part-40

    Part 40

  • 44:24

    Logging, Grafana, Prometheus and Sentry | Production Logging Every Developer Must Know | Part-41

    Part 41

View all 58 lessons
View on GitHub

Part 39 — Files in the AI Era (One Mental Model for Every File)

Parts 36–38.1 taught open(), safe writes, and the structured formats (JSON, JSONL, CSV). This part is not a tour of ten libraries. It gives you one reflex so you never fear a file format again:

When I face any file, I ask two things: do I need a parser, and which one do I install?

Get this reflex and you can open anything in 2026 — Markdown agent memory, .env secrets, PDF, Excel, an image for GPT-4o — including formats nobody has invented yet.


The Mental Model — open() + maybe a parser

Every file comes off the disk as raw bytes. Two translation layers turn those bytes into something you can use:

file on disk  →  open()  →  text  →  parser  →  Python object
                   ↑                    ↑
              encoding (UTF-8)     library (json, pypdf, Pillow…)
              bytes → characters   text/bytes → dict, image, table
  • **open()** is always the door. With an encoding (UTF-8) it turns bytes into text. — Part 36.
  • A parser is a library that turns that text/bytes into a richer object (a dict, an image, a table).

The key insight:

FileNeed a parser?Why
Plain text (.txt, .md)Notext is already a usable Python str — you stop at open()
Anything structured (.json, .pdf, image…)Yesyou want more than a string, so a parser interprets the format

So open() is the common skill. The parser is the only thing that changes — and someone already wrote it so you just call 3 functions.


The Hero Table — Which File Needs Which Parser

This one table is the whole part. Memorize the reflex, not the rows:

File / formatNeed a parser?LibraryBuilt-in or pip?You get back (Python object)
.txt plain textNo — open() + encoding—built-instr (text)
.md MarkdownNo (plain text; parser only for metadata)python-frontmatterpip (optional)str (text)
.jsonYesjsonbuilt-indict / list
.csvYescsvbuilt-inlist of rows / dicts
.tomlYes (read)tomllibbuilt-in (3.11+)dict
.yamlYespyyamlpipdict
.envYespython-dotenvpipenv vars → str via os.getenv
.pdfYespypdfpippage objects → text
.xlsx ExcelYesopenpyxlpipworkbook → sheets → cells
.jpg / .png imagesYesPillowpipImage object
.db SQLiteYessqlite3built-inquery result rows

Reading the table is the skill: "Markdown? just open it. YAML? pip install pyyaml. PDF? pip install pypdf." You already proved this with json and csv in Part 38.1 — now you see the pattern is the same for every format.


Four Quick Proofs (install the lib, call 3 functions)

These are not tutorials — they show the reflex in action for the four files an AI engineer touches most.

1. Markdown — NO parser (this is AI agent memory)

from pathlib import Path

MEMORY = Path("agent_memory.md")

with open(MEMORY, "a", encoding="utf-8") as f:   # just open + encoding — no parser
    f.write("- User prefers Kannada explanations\n")

print(MEMORY.read_text(encoding="utf-8"))

That is the core of every AI agent's memory — Cursor (AGENTS.md), Claude Code (CLAUDE.md) all read a Markdown file each turn. Plain text, no parser needed.

2. .env — parser python-dotenv (your API keys)

# pip install python-dotenv
from dotenv import load_dotenv
import os

load_dotenv()                       # the parser reads .env into the environment
api_key = os.getenv("OPENAI_API_KEY")
# .env  ← NEVER commit. Add it to .gitignore.
OPENAI_API_KEY=sk-proj-...

Real story: GitHub scans public repos for key patterns. Push a .env by accident and a bot can drain $200–$2000 of credits in 30 minutes. .gitignore it before your first commit.

3. PDF — parser pypdf (the start of every "chat with your PDF")

# pip install pypdf
from pypdf import PdfReader

reader = PdfReader("contract.pdf")                       # parser understands PDF
text = "\n".join(p.extract_text() or "" for p in reader.pages)
# now feed `text` to an LLM → summary, Q&A, RAG

PDF text is messy (tables, columns). For heavy work use pymupdf; for scanned PDFs (images) you need OCR (pytesseract).

4. Image — parser Pillow + base64 (multimodal AI)

# pip install pillow
from PIL import Image
import base64, io

img = Image.open("invoice.jpg")     # Pillow parses the image bytes
img.thumbnail((1024, 1024))         # shrink → lower API cost

buf = io.BytesIO()                  # in-memory file (Part 38)
img.save(buf, format="JPEG")
encoded = base64.b64encode(buf.getvalue()).decode("ascii")
# send `encoded` to a multimodal LLM (GPT-4o, Claude, Gemini)

This is exactly how receipt scanners and "explain this chart" tools send an image to a model.

YAML / TOML / Excel / SQLite follow the same reflex — find the row in the table, install the library, call its read/write functions. No new skill, just a different parser.


Three Gotchas Worth Remembering

GotchaFix
.env pushed to GitHubrevoke the key, rotate it, add .env to .gitignore
yaml.load() on an untrusted filealways use yaml.safe_load() (the unsafe one runs code)
Excel shows =SUM(...) instead of valuesload_workbook(..., data_only=True)

Practice — Mini AI Memory CLI

Build the persistent layer of an AI agent (the LLM comes in Part 70). Create src/memory_cli.py:

  1. **remember(fact)** — append - fact to memory.md (no parser — plain text)
  2. **recall()** — read and print memory.md
  3. **export_jsonl(path)** — write each fact as one JSONL line (parser: json)
  4. **load_secrets()** — read .env, confirm OPENAI_API_KEY is set without printing it (parser: python-dotenv)

You'll touch a no-parser file (Markdown), a built-in parser (json), and a pip parser (dotenv) — the whole mental model in one exercise.


AI Job ಸಿಗಲ್ಲಾ ! ಈ 1 Format ಗೂಥಿಲ್ಲಾ ಅಂದ್ರೆ (JSON vs JSONL vs CSV) | Part-38.1Production-Level Debugging: Find Bugs Without Guessing | Python in Kannada | Part-40

Up Next

41 / 58
  • 20:17

    Production-Level Debugging: Find Bugs Without Guessing | Python in Kannada | Part-40

    Part 40

  • 44:24

    Logging, Grafana, Prometheus and Sentry | Production Logging Every Developer Must Know | Part-41

    Part 41

View all 58 lessons

GitHub Notes

View Part 39 notes on GitHub
GitHub Notes
View Part 39 notes on GitHub
PrevNext
15:56
15:56

Why Production Code Needs OOP Thinking | Functions ಸಕಗಲ್ವಾ ? | OOP Master Flow | Part-42

Part 42

Why Production Code Needs OOP Thinking | Functions ಸಕಗಲ್ವಾ ? | OOP Master Flow | Part-42

Part 42

  • 36:29

    Production-ನಲ್ಲಿ Class Use ಮಾಡೋ Moment ಯಾವುದು? | OOP Master Flow | Part-43

    Part 43

  • 36:29

    Production-ನಲ್ಲಿ Class Use ಮಾಡೋ Moment ಯಾವುದು? | OOP Master Flow | Part-43

    Part 43

  • 17:16

    Encapsulation Truth No One Taught You | OOP Master Flow | Part-44

    Part 44

  • 17:16

    Encapsulation Truth No One Taught You | OOP Master Flow | Part-44

    Part 44

  • 19:43

    Best Developers ಎಲ್ಲಾ Lazy?🔥 Python Inheritance in Kannada | OOP Master Flow | Part-45

    Part 45

  • 19:43

    Best Developers ಎಲ್ಲಾ Lazy?🔥 Python Inheritance in Kannada | OOP Master Flow | Part-45

    Part 45

  • 14:19

    Production Codeನಲ್ಲಿ Polymorphism ಯಾಕೆ Powerful ? OOP Master Flow | Part-46

    Part 46

  • 14:19

    Production Codeನಲ್ಲಿ Polymorphism ಯಾಕೆ Powerful ? OOP Master Flow | Part-46

    Part 46

  • 19:38

    Understand Abstraction Like a Senior Developer | OOP Master Flow | Part-47

    Part 47

  • 19:38

    Understand Abstraction Like a Senior Developer | OOP Master Flow | Part-47

    Part 47