Read a File's Content
Exit

Read a File's Content

Add a function that reads any text file and silently skips files it can't read

💻

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

Before the assistant can index a file, it needs to read the file's text. Most files in a typical project folder open without issue — but two categories of files will cause the program to crash if you don't handle them explicitly:

  • Binary files: Images, compiled binaries, and similar files contain bytes that aren't valid UTF-8. Reading them with a text decoder raises UnicodeDecodeError.
  • Locked or inaccessible files: Files that are open by another process, or that you don't have permission to read, raise OSError.

Rather than stopping the entire indexing run when one file fails, read_file catches both errors and returns None. The caller — index_folder, which you'll write in the next chapter — checks for None and skips that file with continue.

This pattern keeps the assistant resilient: a single unreadable file in a folder of 200 won't prevent the other 199 from being indexed.

Instructions

  1. Define a function called read_file that takes path.
  2. Inside a try block, open path with open(path, "r", encoding="utf-8"). Return the file's full text using .read().
  3. Add an except (UnicodeDecodeError, OSError): clause that returns None.