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 MemoryFS constructor takes no arguments.

Example

>>> mem_fs = MemoryFS()
Or via an FS URL:
>>> import fs
>>> mem_fs = fs.open_fs('mem://')
close()[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 FilesystemClosed exception will be thrown.

getinfo(path, namespaces=None)[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 (defaults to [basic]).
Returns:

resource information object.

Return type:

Info

For more information regarding resource information, see Resource Info.

listdir(path)[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:
makedir(path, permissions=None, recreate=False)[source]

Make a directory.

Parameters:
  • path (str) – Path to directory from root.
  • permissions (Permissions, optional) – a Permissions instance, or None to use default.
  • recreate (bool) – Set to True to avoid raising an error if the directory already exists (defaults to False).
Returns:

a filesystem whose root is the new directory.

Return type:

SubFS

Raises:
openbin(path, mode='r', buffering=-1, **options)[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 b in 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:
remove(path)[source]

Remove a file from the filesystem.

Parameters:

path (str) – Path of the file to remove.

Raises:
removedir(path)[source]

Remove a directory from the filesystem.

Parameters:

path (str) – Path of the directory to remove.

Raises:
setinfo(path, info)[source]

Set info on a resource.

This method is the complement to getinfo and 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 path does not exist on the filesystem

The info dict should be in the same format as the raw info returned by getinfo(file).raw.

Example

>>> details_info = {"details": {
...     "modified": time.time()
... }}
>>> my_fs.setinfo('file.txt', details_info)