Bird
0
0

Why does this code raise an error?

medium📝 Debug Q7 of 15
LangChain - Document Loading
Why does this code raise an error?
loader = DirectoryLoader('docs', glob='*.txt')
docs = loader.load()
print(docs[0].page_content)
ADocuments do not have 'page_content' attribute
Bdocs is empty because no .txt files exist
Cload() returns a dict, not a list
Dprint statement syntax is incorrect
Step-by-Step Solution
Solution:
  1. Step 1: Understand what causes index error

    If docs is empty, accessing docs[0] causes an error.
  2. Step 2: Check possible reasons for empty docs

    No .txt files in 'docs' folder means docs list is empty.
  3. Final Answer:

    docs is empty because no .txt files exist -> Option B
  4. Quick Check:

    Empty docs list causes index error [OK]
Quick Trick: Check if docs list is empty before accessing elements [OK]
Common Mistakes:
  • Assuming docs is dict
  • Thinking page_content attribute missing
  • Blaming print syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LangChain Quizzes