A directory loader helps you quickly load many documents from a folder all at once. It saves time by handling multiple files together instead of one by one.
Directory loader for bulk documents in LangChain
from langchain.document_loaders import DirectoryLoader loader = DirectoryLoader('path/to/folder', glob='**/*.txt') documents = loader.load()
The DirectoryLoader takes a folder path and an optional glob pattern to select file types.
The load() method reads all matching files and returns a list of documents.
from langchain.document_loaders import DirectoryLoader loader = DirectoryLoader('data/docs', glob='**/*.pdf') docs = loader.load()
loader = DirectoryLoader('notes', glob='*.md') docs = loader.load()
loader = DirectoryLoader('articles')
docs = loader.load()This example loads all text files from the 'my_documents' folder and prints how many were loaded. It also shows a preview of the first document's content.
from langchain.document_loaders import DirectoryLoader # Create a loader for all text files in 'my_documents' loader = DirectoryLoader('my_documents', glob='**/*.txt') # Load all documents documents = loader.load() # Print the number of documents loaded print(f"Loaded {len(documents)} documents.") # Print the first 100 characters of the first document if documents: print("First document preview:") print(documents[0].page_content[:100])
Make sure the folder path is correct and accessible.
The glob pattern helps filter file types, like '*.txt' or '**/*.pdf'.
Documents are returned as a list of objects with a page_content attribute holding the text.
DirectoryLoader loads many files from a folder at once.
Use glob to pick specific file types.
Call load() to get all documents as a list.
Practice
DirectoryLoader in Langchain?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 CQuick Check:
DirectoryLoader loads many files = A [OK]
- Confusing loading with saving files
- Thinking it converts file formats
- Assuming it creates directories
DirectoryLoader?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 AQuick Check:
Use '*.txt' to pick text files [OK]
- Using folder names instead of file patterns
- Using wrong file extensions
- Confusing recursive patterns with simple glob
loader = DirectoryLoader('docs', glob='*.md')
docs = loader.load()What will
docs contain?Solution
Step 1: Analyze DirectoryLoader usage
The DirectoryLoader is set to load files from 'docs' folder with glob '*.md', meaning markdown files only.Step 2: Understand load() output
The load() method returns a list of documents matching the glob pattern, so docs will be a list of markdown files.Final Answer:
A list of all markdown (.md) documents in the 'docs' folder -> Option AQuick Check:
load() returns list of files matching glob [OK]
- Expecting a single document instead of a list
- Assuming glob pattern is ignored
- Thinking load() returns file paths instead of documents
loader = DirectoryLoader('files', glob='*.txt')
docs = loader.loadSolution
Step 1: Check method call syntax
The load method must be called with parentheses: load()Step 2: Verify other parts
Glob pattern '*.txt' is correct, DirectoryLoader accepts relative paths, and load() method exists.Final Answer:
Missing parentheses after load method call -> Option BQuick Check:
Method calls need () to execute [OK]
- Forgetting parentheses on method calls
- Assuming glob pattern is wrong
- Thinking DirectoryLoader needs absolute paths
Solution
Step 1: Understand glob pattern limitations
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.Final Answer:
loader = DirectoryLoader('resources', glob='*.pdf') loader2 = DirectoryLoader('resources', glob='*.txt') docs = loader.load() + loader2.load() -> Option DQuick Check:
Use separate loaders for multiple file types [OK]
- Trying to use multiple patterns in one glob string
- Assuming glob supports ';' or '|' separators
- Loading all files without filtering
Ready to prove it?
Pass the test and this pattern moves unknown
