Filesystem Provider
The filesystem provider stores each key as a file inside a chosen folder.
Installation
Section titled “Installation”No extra dependencies are required beyond cshelve itself:
pip install cshelveConfiguration Options
Section titled “Configuration Options”| Scope | Option | Description | Default Value | Required |
|---|---|---|---|---|
default | folder_path | Root folder where keys are stored. Absolute paths are used as-is. | . | No |
default | encoding | Text encoding used to decode key bytes into path segments. | utf-8 | No |
Behavior and Notes
Section titled “Behavior and Notes”- Creation (
create): Createsfolder_path(and parents) if it does not exist. If the folder already exists,create()is a no-op. If a non-directory exists at that path, aConfigurationErroris raised. - Delete semantics:
delete()removes only the file for the key and intentionally leaves any empty parent directories. This avoids extra parsing and plays better with concurrent access. - Iteration/len: Recursively walks the root folder and yields keys with POSIX separators (
/). - Encoding: Keep
encodingconsistent between writers and readers. Use a compatible encoding (e.g.,utf-8,utf-16) if your keys include non-ASCII characters. - Thread safety: No locking is applied; callers should coordinate concurrent writes if needed.
Example INI Configuration
Section titled “Example INI Configuration”[default]provider = filesystemfolder_path = ./data/cache # relative to the current working directoryencoding = utf-8 # optional
[provider_params]# You can override folder_path/encoding here; values in [default] win when present.Absolute Path Example
Section titled “Absolute Path Example”[default]provider = filesystemfolder_path = /tmp/tmp_filePython Usage
Section titled “Python Usage”import cshelve
# Using the INI configurationwith cshelve.open('filesystem.ini') as db: db['bonjour'] = 'hello' assert db['bonjour'] == 'hello'Troubleshooting
Section titled “Troubleshooting”- Permission denied: Ensure the process can create and write to
folder_path(and its parents). On Unix, check directory ownership and mode bits; on Windows, ensure the process has write permissions. - Non-directory at folder_path: If a file already exists where
folder_pathshould be,create()will raise aConfigurationError. Remove or rename the conflicting file, or choose another folder. - Encoding errors: If decoding issues occur with keys, align the
encodingsetting between writers and readers. Prefer UTF-8 unless there is a specific need for another encoding.