Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

directory_file_mapping-0.1.0a0.tar.gz (3.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

directory_file_mapping-0.1.0a0-py2.py3-none-any.whl (4.2 kB view details)

Uploaded Python 2Python 3

File details

Details for the file directory_file_mapping-0.1.0a0.tar.gz.

File metadata

File hashes

Hashes for directory_file_mapping-0.1.0a0.tar.gz
Algorithm Hash digest
SHA256 e06927e23dab6324150261cbba50a6ff2d81f30d733fdbc8a40ccfcbc024bb87
MD5 b2302feca928ab58e6451baaa526d314
BLAKE2b-256 a1e1ce10a22d1609445b0f1a5f4a1166e1201edee079729883b7eb8808a7c5b4

See more details on using hashes here.

File details

Details for the file directory_file_mapping-0.1.0a0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for directory_file_mapping-0.1.0a0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 55419f01cb723bd26ddf94adc3603af4866e3a34d887cf45aa474bffc166d343
MD5 151128f7babfa42c80951c3cba1de1fc
BLAKE2b-256 0854fdd413813e26a6b735e4822ca0097e14597b23f3920aca49b0055366d064

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page