Sub Filesystem

Manage a directory in a parent filesystem.

class fs.subfs.SubFS(parent_fs, path)[source]

A sub-directory on another filesystem.

A SubFS is a filesystem object that maps to a sub-directory of another filesystem. This is the object that is returned by opendir.


Get the proxied filesystem.

This method should return a filesystem for methods not associated with a path, e.g. getmeta.


Encode a path for proxied filesystem.

Parameters:path (str) – A path on the filesystem.
Returns:a tuple of (<filesystem>, <new_path>)
Return type:(FS, str)
class fs.subfs.ClosingSubFS(parent_fs, path)[source]

A version of SubFS which closes its parent when closed.


Close the filesystem and release any resources.

It is important to call this method when you have finished working with the filesystem. Some filesystems may not finalize changes until they are closed (archives for example). You may call this method explicitly (it is safe to call close multiple times), or you can use the filesystem as a context manager to automatically close.


>>> with OSFS('~/Desktop') as desktop_fs:
...    desktop_fs.writetext(
...        'note.txt',
...        "Don't forget to tape Game of Thrones"
...    )

If you attempt to use a filesystem that has been closed, a FilesystemClosed exception will be thrown.