📖 Scenario: You are building a simple tool to load many text documents from a folder all at once. This helps you quickly gather all your notes or files for processing.
🎯 Goal: Create a Python script using langchain that loads all text files from a directory using DirectoryLoader.
📋 What You'll Learn
Create a variable folder_path with the exact string "./documents"
Create a DirectoryLoader instance called loader using folder_path and the TextLoader class
Use the loader.load() method to load all documents into a variable called documents
Add a line to print the number of documents loaded using len(documents)
💡 Why This Matters
🌍 Real World
Loading many documents at once is useful for building search engines, chatbots, or data analysis tools that work with large text collections.
💼 Career
Understanding how to bulk load documents is important for roles in data science, machine learning, and software development involving text processing.
Progress0 / 4 steps
1
Set the folder path
Create a variable called folder_path and set it to the string "./documents".
LangChain
Hint
Use quotes around the folder path string exactly as shown.
2
Create the DirectoryLoader instance
Import DirectoryLoader and TextLoader from langchain.document_loaders. Then create a variable called loader that is a DirectoryLoader using folder_path and TextLoader as the loader class.
LangChain
Hint
Use loader_cls=TextLoader as the argument to specify the loader class.
3
Load documents from the directory
Use the load() method on loader to load all documents and save them in a variable called documents.
LangChain
Hint
Call loader.load() and assign the result to documents.
4
Print the number of loaded documents
Add a line to print the number of documents loaded by printing len(documents).
LangChain
Hint
Use print(len(documents)) to show how many documents were loaded.
Practice
(1/5)
1. What is the main purpose of the DirectoryLoader in Langchain?
easy
A. To convert documents into text format
B. To save documents to a directory
C. To load multiple documents from a folder at once
D. To create a new directory for documents
Solution
Step 1: Understand DirectoryLoader's role
The DirectoryLoader is designed to read many files from a folder in one go.
Step 2: Compare options with this role
Only To load multiple documents from a folder at once describes loading multiple documents from a folder, matching DirectoryLoader's purpose.
Final Answer:
To load multiple documents from a folder at once -> Option C
Quick Check:
DirectoryLoader loads many files = A [OK]
Hint: DirectoryLoader reads many files from a folder at once [OK]
Common Mistakes:
Confusing loading with saving files
Thinking it converts file formats
Assuming it creates directories
2. Which of the following is the correct way to specify a glob pattern to load only text files using DirectoryLoader?
easy
A. glob='*.txt'
B. glob='text/*'
C. glob='**/*.doc'
D. glob='*.pdf'
Solution
Step 1: Understand glob pattern for text files
The pattern '*.txt' matches all files ending with .txt in the directory.
Step 2: Evaluate each option
glob='*.txt' correctly uses '*.txt' to select text files. Others select different or incorrect file types.
Final Answer:
glob='*.txt' -> Option A
Quick Check:
Use '*.txt' to pick text files [OK]
Hint: Use '*.txt' to select text files with glob [OK]
DirectoryLoader's glob does not support multiple patterns separated by ';' or '|'.
Step 2: Check each option
loader = DirectoryLoader('resources', glob='*.pdf')
loader2 = DirectoryLoader('resources', glob='*.txt')
docs = loader.load() + loader2.load() loads PDFs and text files separately and combines results, which works correctly. loader = DirectoryLoader('resources')
docs = loader.load() # loads all files loads all files but not filtered. Options A and C use invalid glob patterns.