A simple Python class that lets you treat the files in a directory as a mapping object, like a dict.
Project description
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file directory_file_mapping-0.1.0a0.tar.gz.
File metadata
- Download URL: directory_file_mapping-0.1.0a0.tar.gz
- Upload date:
- Size: 3.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e06927e23dab6324150261cbba50a6ff2d81f30d733fdbc8a40ccfcbc024bb87
|
|
| MD5 |
b2302feca928ab58e6451baaa526d314
|
|
| BLAKE2b-256 |
a1e1ce10a22d1609445b0f1a5f4a1166e1201edee079729883b7eb8808a7c5b4
|
File details
Details for the file directory_file_mapping-0.1.0a0-py2.py3-none-any.whl.
File metadata
- Download URL: directory_file_mapping-0.1.0a0-py2.py3-none-any.whl
- Upload date:
- Size: 4.2 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
55419f01cb723bd26ddf94adc3603af4866e3a34d887cf45aa474bffc166d343
|
|
| MD5 |
151128f7babfa42c80951c3cba1de1fc
|
|
| BLAKE2b-256 |
0854fdd413813e26a6b735e4822ca0097e14597b23f3920aca49b0055366d064
|