High severity beginner · Fix: 2-5 min

FileNotFoundError

builtins.FileNotFoundError

What this error means
LlamaIndex raises FileNotFoundError when the specified persist directory path does not exist or is inaccessible.

Stack trace

traceback
Traceback (most recent call last):
  File "app.py", line 25, in <module>
    storage_context = StorageContext.from_defaults(persist_dir="./storage")
  File "/usr/local/lib/python3.9/site-packages/llama_index/storage/storage_context.py", line 45, in from_defaults
    raise FileNotFoundError(f"Persist directory {persist_dir} not found.")
FileNotFoundError: Persist directory ./storage not found.
QUICK FIX
Create the persist directory on disk before initializing StorageContext with persist_dir.

Why it happens

LlamaIndex requires a valid directory path to persist its index data. If the directory does not exist or the path is incorrect, the library raises a FileNotFoundError. This usually happens when the directory was never created or the path is mistyped.

Detection

Check for FileNotFoundError exceptions when initializing StorageContext with a persist_dir parameter, and verify the directory path exists before running the code.

Causes & fixes

1

The persist directory path does not exist on the filesystem.

✓ Fix

Create the directory manually before running the code or add code to create it programmatically using os.makedirs(persist_dir, exist_ok=True).

2

The persist_dir path is incorrectly specified or has a typo.

✓ Fix

Verify and correct the persist_dir string to point to the correct existing directory path.

3

The application lacks filesystem permissions to access or create the directory.

✓ Fix

Ensure the running user has read/write permissions for the directory path or choose a directory with proper access rights.

Code: broken vs fixed

Broken - triggers the error
python
from llama_index import StorageContext

# This will raise FileNotFoundError if './storage' does not exist
storage_context = StorageContext.from_defaults(persist_dir='./storage')  # Error here
Fixed - works correctly
python
import os
from llama_index import StorageContext

persist_dir = './storage'
os.makedirs(persist_dir, exist_ok=True)  # Create directory if missing
storage_context = StorageContext.from_defaults(persist_dir=persist_dir)  # Fixed
print('StorageContext initialized successfully')
Added os.makedirs to ensure the persist directory exists before initializing StorageContext, preventing FileNotFoundError.

Workaround

Wrap the StorageContext initialization in try/except FileNotFoundError and create the directory dynamically if caught, then retry initialization.

Prevention

Always verify and create the persist directory path during application startup or deployment to avoid runtime directory not found errors.

Python 3.7+ · llama-index >=0.5.0 · tested on 0.6.x
Verified 2026-04
Verify ↗

Community Notes

No notes yetBe the first to share a version-specific fix or tip.