LlamaIndexllama-index-core 0.14 · Python 3.10+
Dashboard
0%
1
Curious builder0 XP earned · 300 to level 2
0 daysFinish a lesson to begin
Badge collection0 of 6 unlocked
30 small wins to finish your pathNext lesson →

SimpleDirectoryReader: loading a folder of documents

SimpleDirectoryReader is a reader that turns every file in a folder into a Document, and the metadata it attaches travels with every chunk from then on.

Last updated: 28 Sep, 2026 · LlamaIndex 0.14

So far the documents were typed into the code. Real help pages live in files, so this lesson loads the shop's help folder from disk, which is where the running example gets its content for the rest of the course.

The help-centre folder

The help folder holds three Markdown files. Here is one; the other two follow the same pattern, with delivery times and the shop's two lamps:

markdown
# Refunds

You can get a full refund within 30 days of delivery. The money goes back to the card you paid with within 5 working days of us receiving the item.

Items bought in a sale can be refunded too, but the delivery charge is not returned.

To start a refund, open the order in your account and choose Return an item. Print the label and drop the parcel at any post office.

Personalised items cannot be refunded unless they arrive damaged.

Loading the folder

Point the reader at the folder and call load_data:

python
from llama_index.core import SimpleDirectoryReader

documents = SimpleDirectoryReader("help").load_data()
Project files used on this pageThis lesson builds on a project from earlier lessons. The code below imports these files. Click a file to see its code, or follow the link to the lesson that wrote it. To run the code yourself, keep them in the same folder.
View the code here
help/lamps.md
# Lamps

The LMP-204 desk lamp has a known cable fault. Stop using a lamp with a damaged cable and we will replace it free of charge.

All lamps come with a two year guarantee against electrical faults.

Bulbs are not covered by the refund policy once they have been used.

The LMP-310 floor lamp needs a bulb with an E27 fitting, which is sold separately.
help/delivery.md
# Delivery

Standard delivery takes 3 to 5 working days and is free on orders over 40.

Express delivery arrives the next working day if you order before 2pm. It costs 6.

We deliver to the mainland only. Parcels to islands take 2 extra working days.

If a parcel has not arrived after 10 working days, contact us and we will send a replacement.

Loading the folder into Documents

Example
from llama_index.core import SimpleDirectoryReader

documents = SimpleDirectoryReader("help").load_data()
print(len(documents))
print(sorted(documents[0].metadata))

Reading the loaded documents

  • Three Documents come back, one per file in the folder.
  • The reader fills in metadata by itself: the file's name, type, size and dates, and its full path on your computer.
  • That path is the problem: unless a key is excluded, its value goes into the embedded text and the model's prompt, so a machine path would leak into both.

Choosing the metadata with a function

Give the reader a file_metadata function that decides what each document carries. This one keeps the file name and drops everything else:

python
import os


def only_name(path):
    return {"file_name": os.path.basename(path)}

Keeping only the file name

Example
import os

from llama_index.core import SimpleDirectoryReader


def only_name(path):
    return {"file_name": os.path.basename(path)}


documents = SimpleDirectoryReader("help", file_metadata=only_name).load_data()

print(len(documents))
for document in documents:
    print(document.metadata)

Reading the trimmed metadata

  • Each document now carries its file name and nothing about the machine it was loaded on.
  • file_metadata is called with each file's path and returns the metadata dictionary for that file.
  • The order follows the folder listing, so the files come back sorted by name.

Default metadata vs a metadata function

Defaultfile_metadata function
Keys addedname, path, type, size, datesOnly what you return
file_path presentYesNo, unless you add it
Leaks machine pathsInto embed and promptNo
You control itNoYes

When to pass a metadata function

  • Any time documents are loaded from a real path, so machine paths do not reach the model.
  • When you want a stable key such as a document id or a section name for filtering later.
  • When the folder mixes formats and you want the same metadata shape for all of them.
Watch out. The reader excludes the file keys it adds from the embedded text, but it leaves file_path in, so the full path from your laptop ends up in what gets embedded and in the prompt. Decide what travels with a file_metadata function before it does.
Try it yourself
  • Add "folder": os.path.basename(os.path.dirname(path)) to the metadata.
  • Pass required_exts=[".md"] and add a notes.txt to the folder.
  • Print documents[0].text[:80].

Every expert started right here.