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.pyhandles everything related to reading files from disk.app.pyhandles 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
- Add
import osat the top offiles.py. - Define
SUPPORTED_EXTENSIONSas a module-level constant set with the same 8 extensions fromapp.py. - Copy
list_files(folder)exactly as it appears inapp.py. - Copy
read_file(path)exactly as it appears inapp.py. - Copy
index_folder(folder, chunk_size=500, overlap=100)exactly as it appears inapp.py.
Interactive Code Editor
Sign in to write and run code, track your progress, and unlock all chapters.
Sign In to Start Coding