Extract to files.py
Exit

Extract to files.py

Move the file I/O functions into a dedicated module

💻

Writing code and entering commands is only available on desktop. Open this page on a larger screen to complete this chapter.

app.py now contains two distinct types of logic: file system operations (list_files, read_file, index_folder) and AI operations (embedding, search, prompting). Mixing them in one file makes both harder to maintain — you have to read past embedding code to find file-reading code, and vice versa.

Splitting by responsibility gives each file a clear job:

  • files.py handles everything related to reading files from disk.
  • app.py handles everything related to the AI pipeline.

This separation makes the codebase easier to navigate and easier to extend. When you add support for a new file type in a later lesson, you'll edit only files.py. When you change how embeddings work, you'll edit only app.py.

In this chapter, you write files.py by copying the four file-related items out of app.py. The next chapter updates app.py to import from files.py instead.

Instructions

  1. Add import os at the top of files.py.
  2. Define SUPPORTED_EXTENSIONS as a module-level constant set with the same 8 extensions from app.py.
  3. Copy list_files(folder) exactly as it appears in app.py.
  4. Copy read_file(path) exactly as it appears in app.py.
  5. Copy index_folder(folder, chunk_size=500, overlap=100) exactly as it appears in app.py.