This release is a pre-release and may not be stable for production use.
directory-file-mapping
Ever wanted to treat a directory as a Python dict? Now you can!
A simple Python class that lets you treat the files in a directory as a mapping object, like a dict. Filenames are keys, and file contents (as bytes) are values. Changes you make to the mapping immediately update the actual files in the filesystem. Yes, this works for reading, writing, listing, and deleting files, all using familiar mapping syntax!
It seems almost obvious in retrospect, but neither collections, pathlib, nor anyone in the Python community appears
to have shipped a general dict-like view of a directory. But now you never need to write a kludge of manual
os.listdir, open(), and os.remove loops again.
Features
- Mapping interface: Get, set, iterate keys, and delete by key, just like a dict.
- Filenames as keys.
- File contents as values (bytes).
- Acts instantly on the filesystem: Any change is reflected (and visible to other processes) right away.
- No caching: Reads contents on every access, always up to date.
Install
pip install directory-file-mapping
Usage
from directory_file_mapping import DirectoryFileMapping
# Initialize mapping to a directory
m = DirectoryFileMapping('/tmp/exampledir')
# Write new file
m['foo.txt'] = b'hello world'
# Read file contents
data = m['foo.txt'] # b'hello world'
# Check existence
if 'bar.txt' in m:
print(m['bar.txt'])
# List filenames
for name in m:
print(name)
# Delete a file
del m['foo.txt']
Caveats & Limitations
- This mapping is volatile: other processes may add, remove, or change files under your feet.
- File names and contents are not cached.
- This mapping only exposes regular files, not subdirectories or special files.
- It does not filter out unusually named files or tamper with binary vs. text.
- No atomicity guarantees: if your coworker is deleting files while you iterate, expect surprises.
Contributing
Contributions are welcome! Please submit pull requests or open issues on the GitHub repository.
License
This project is licensed under the MIT License.
Release files for directory-file-mapping 0.1.0a0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| directory_file_mapping-0.1.0a0.tar.gz | 3.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| directory_file_mapping-0.1.0a0-py2.py3-none-any.whl | Python 3, Python 2 | none | any | Details |
Total release size: 8.0 kB
Release files / directory_file_mapping-0.1.0a0.tar.gz
| Download URL | directory_file_mapping-0.1.0a0.tar.gz |
|---|---|
| Size | 3.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e06927e23dab6324150261cbba50a6ff2d81f30d733fdbc8a40ccfcbc024bb87
|
|
BLAKE2b-256 checksum How to use checksums |
a1e1ce10a22d1609445b0f1a5f4a1166e1201edee079729883b7eb8808a7c5b4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.9
|
Release files / directory_file_mapping-0.1.0a0-py2.py3-none-any.whl
| Download URL | directory_file_mapping-0.1.0a0-py2.py3-none-any.whl |
|---|---|
| Size | 4.2 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
55419f01cb723bd26ddf94adc3603af4866e3a34d887cf45aa474bffc166d343
|
|
BLAKE2b-256 checksum How to use checksums |
0854fdd413813e26a6b735e4822ca0097e14597b23f3920aca49b0055366d064
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.9
|