A MultiFS is a filesystem composed of a sequence of other filesystems, where the directory structure of each filesystem is overlaid over the previous filesystem. When you attempt to access a file from the MultiFS it will try each ‘child’ FS in order, until it either finds a path that exists or raises a ResourceNotFoundError.
One use for such a filesystem would be to selectively override a set of files, to customize behavior. For example, to create a filesystem that could be used to theme a web application. We start with the following directories:
`-- templates |-- snippets | `-- panel.html |-- index.html |-- profile.html `-- base.html `-- theme |-- snippets | |-- widget.html | `-- extra.html |-- index.html `-- theme.html
And we want to create a single filesystem that looks for files in templates if they don’t exist in theme. We can do this with the following code:
from fs.osfs import OSFS from fs.multifs import MultiFS themed_template_fs = MultiFS() themed_template_fs.addfs('templates', OSFS('templates')) themed_template_fs.addfs('theme', OSFS('theme'))
Now we have a themed_template_fs FS object presents a single view of both directories:
|-- snippets | |-- panel.html | |-- widget.html | `-- extra.html |-- index.html |-- profile.html |-- base.html `-- theme.html
A MultiFS is generally read-only, and any operation that may modify data (including opening files for writing) will fail. However, you can set a writeable fs with the setwritefs method – which does not have to be one of the FS objects set with addfs.
The reason that only one FS object is ever considered for write access is that otherwise it would be ambiguous as to which filesystem you would want to modify. If you need to be able to modify more than one FS in the MultiFS, you can always access them directly.
A filesystem that delegates to a sequence of other filesystems.
Operations on the MultiFS will try each ‘child’ filesystem in order, until it succeeds. In effect, creating a filesystem that combines the files and dirs of its children.
Parameters: auto_close – If True the child filesystems will be closed when the MultiFS is closed
Adds a filesystem to the MultiFS.
- name – A unique name to refer to the filesystem being added. The filesystem can later be retrieved by using this name as an index to the MultiFS, i.e. multifs[‘myfs’]
- fs – The filesystem to add
- write – If this value is True, then the fs will be used as the writeable FS
- priority – A number that gives the priorty of the filesystem being added. Filesystems will be searched in descending priority order and then by the reverse order they were added. So by default, the most recently added filesystem will be looked at first
Clears the writeable filesystem (operations that modify the multifs will fail)
Removes a filesystem from the sequence.
Parameters: name – The name of the filesystem, as used in addfs
Sets the filesystem to use when write access is required. Without a writeable FS, any operations that could modify data (including opening files for writing / appending) will fail.
Parameters: fs – An FS object that will be used to open writeable files
Retrieves the filesystem that a given path would delegate to. Returns a tuple of the filesystem’s name and the filesystem object itself.
Parameters: path – A path in MultiFS