toolrack.fsmap¶
Access the filesystem in a dict-like fashion.
This module provides a Directory
class which provides access to the
filesystem subtree below its path, allow accessing files and sub-directories as
elements of a dict (e.g. directory['foo']
or directory['foo/bar']
).
-
toolrack.fsmap.
DIR
= <object object>¶ Marker for creating directories.
-
class
toolrack.fsmap.
Directory
(path)¶ Provide access to the sub-tree of a directrory.
It represents a directory in the filesystem:
directory = Directory('/base/path')
The object is iterable and yields names of contained elements:
for elem in directory: do_something(directory[elem])
Sub-directories and files below the base path can be accessed as items of a dict. For instance:
directory['a-dir']['a-file']
or even with a single access, using OS path format:
directory['a-dir/a-file']
Path elements can be removed with
del
:del directory['a-file'] del directory['a-dir'] # this will delete the whole sub-tree
Files are created/overwritten by assiging content:
directory['a-file'] = 'some content'
and directories are created using the
DIR
marker:directory['a-new-dir'] = DIR