Memory Filesystem¶
Manage a volatile in-memory filesystem.
- class fs.memoryfs.MemoryFS[source]¶
A filesystem that stored in memory.
Memory filesystems are useful for caches, temporary data stores, unit testing, etc. Since all the data is in memory, they are very fast, but non-permanent. The
MemoryFSconstructor takes no arguments.Examples
Create with the constructor:
>>> from fs.memoryfs import MemoryFS >>> mem_fs = MemoryFS()
Or via an FS URL:
>>> import fs >>> mem_fs = fs.open_fs('mem://')
- close() None[source]¶
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.
Example
>>> 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
FilesystemClosedexception will be thrown.
- getinfo(path: Text, namespaces: Collection[Text] | None = None) Info[source]¶
Get information about a resource on a filesystem.
- Parameters:
path (str) – A path to a resource on the filesystem.
namespaces (list, optional) – Info namespaces to query. The
"basic"namespace is alway included in the returned info, whatever the value ofnamespacesmay be.
- Returns:
resource information object.
- Return type:
- Raises:
fs.errors.ResourceNotFound – If
pathdoes not exist.
For more information regarding resource information, see Resource Info.
- listdir(path: Text) List[Text][source]¶
Get a list of the resource names in a directory.
This method will return a list of the resources in a directory. A resource is a file, directory, or one of the other types defined in
ResourceType.- Parameters:
path (str) – A path to a directory on the filesystem
- Returns:
list of names, relative to
path.- Return type:
list
- Raises:
fs.errors.DirectoryExpected – If
pathis not a directory.fs.errors.ResourceNotFound – If
pathdoes not exist.
- makedir(path: Text, permissions: Permissions | None = None, recreate: bool = False) SubFS[_M][source]¶
Make a directory.
- Parameters:
path (str) – Path to directory from root.
permissions (Permissions, optional) – a
Permissionsinstance, orNoneto use default.recreate (bool) – Set to
Trueto avoid raising an error if the directory already exists (defaults toFalse).
- Returns:
a filesystem whose root is the new directory.
- Return type:
- Raises:
fs.errors.DirectoryExists – If the path already exists.
fs.errors.ResourceNotFound – If the path is not found.
- move(src_path, dst_path, overwrite=False, preserve_time=False)[source]¶
Move a file from
src_pathtodst_path.- Parameters:
src_path (str) – A path on the filesystem to move.
dst_path (str) – A path on the filesystem where the source file will be written to.
overwrite (bool) – If
True, destination path will be overwritten if it exists.preserve_time (bool) – If
True, try to preserve mtime of the resources (defaults toFalse).
- Raises:
fs.errors.FileExpected – If
src_pathmaps to a directory instead of a file.fs.errors.DestinationExists – If
dst_pathexists, andoverwriteisFalse.fs.errors.ResourceNotFound – If a parent directory of
dst_pathdoes not exist.
- movedir(src_path, dst_path, create=False, preserve_time=False)[source]¶
Move directory
src_pathtodst_path.- Parameters:
src_path (str) – Path of source directory on the filesystem.
dst_path (str) – Path to destination directory.
create (bool) – If
True, thendst_pathwill be created if it doesn’t exist already (defaults toFalse).preserve_time (bool) – If
True, try to preserve mtime of the resources (defaults toFalse).
- Raises:
fs.errors.ResourceNotFound – if
dst_pathdoes not exist, andcreateisFalse.fs.errors.DirectoryExpected – if
src_pathor one of its ancestors is not a directory.
- openbin(path: Text, mode: Text = 'r', buffering: int = -1, **options: Any) BinaryIO[source]¶
Open a binary file-like object.
- Parameters:
path (str) – A path on the filesystem.
mode (str) – Mode to open file (must be a valid non-text mode, defaults to r). Since this method only opens binary files, the
bin the mode string is implied.buffering (int) – Buffering policy (-1 to use default buffering, 0 to disable buffering, or any positive integer to indicate a buffer size).
**options – keyword arguments for any additional information required by the filesystem (if any).
- Returns:
a file-like object.
- Return type:
io.IOBase
- Raises:
fs.errors.FileExpected – If
pathexists and is not a file.fs.errors.FileExists – If the
pathexists, and exclusive mode is specified (xin the mode).fs.errors.ResourceNotFound – If
pathdoes not exist andmodedoes not imply creating the file, or if any ancestor ofpathdoes not exist.
- remove(path: Text) None[source]¶
Remove a file from the filesystem.
- Parameters:
path (str) – Path of the file to remove.
- Raises:
fs.errors.FileExpected – If the path is a directory.
fs.errors.ResourceNotFound – If the path does not exist.
- removedir(path: Text) None[source]¶
Remove a directory from the filesystem.
- Parameters:
path (str) – Path of the directory to remove.
- Raises:
fs.errors.DirectoryNotEmpty – If the directory is not empty ( see
removetreefor a way to remove the directory contents).fs.errors.DirectoryExpected – If the path does not refer to a directory.
fs.errors.ResourceNotFound – If no resource exists at the given path.
fs.errors.RemoveRootError – If an attempt is made to remove the root directory (i.e.
'/')
- removetree(path: Text) None[source]¶
Recursively remove a directory and all its contents.
This method is similar to
removedir, but will remove the contents of the directory if it is not empty.- Parameters:
dir_path (str) – Path to a directory on the filesystem.
- Raises:
fs.errors.ResourceNotFound – If
dir_pathdoes not exist.fs.errors.DirectoryExpected – If
dir_pathis not a directory.
Caution
A filesystem should never delete its root folder, so
FS.removetree("/")has different semantics: the contents of the root folder will be deleted, but the root will be untouched:>>> home_fs = fs.open_fs("~") >>> home_fs.removetree("/") >>> home_fs.exists("/") True >>> home_fs.isempty("/") True
Combined with
opendir, this can be used to clear a directory without removing the directory itself:>>> home_fs = fs.open_fs("~") >>> home_fs.opendir("/Videos").removetree("/") >>> home_fs.exists("/Videos") True >>> home_fs.isempty("/Videos") True
- scandir(path: Text, namespaces: Collection[Text] | None = None, page: Tuple[int, int] | None = None) Iterator[Info][source]¶
Get an iterator of resource info.
- Parameters:
path (str) – A path to a directory on the filesystem.
namespaces (list, optional) – A list of namespaces to include in the resource information, e.g.
['basic', 'access'].page (tuple, optional) – May be a tuple of
(<start>, <end>)indexes to return an iterator of a subset of the resource info, orNoneto iterate over the entire directory. Paging a directory scan may be necessary for very large directories.
- Returns:
an iterator of
Infoobjects.- Return type:
Iterator
- Raises:
fs.errors.DirectoryExpected – If
pathis not a directory.fs.errors.ResourceNotFound – If
pathdoes not exist.
- setinfo(path: Text, info: RawInfo) None[source]¶
Set info on a resource.
This method is the complement to
getinfoand is used to set info values on a resource.- Parameters:
path (str) – Path to a resource on the filesystem.
info (dict) – Dictionary of resource info.
- Raises:
fs.errors.ResourceNotFound – If
pathdoes not exist on the filesystem
The
infodict should be in the same format as the raw info returned bygetinfo(file).raw.Example
>>> details_info = {"details": { ... "modified": time.time() ... }} >>> my_fs.setinfo('file.txt', details_info)