Filesystem access
Skills have access to both persistent and temporary namespaced filesystems independent of the Skill's root directory.
Many Skills may want access to parts of the filesystem. To account for the many different platforms that can run Mycroft there are three locations that a Skill can utilize.
Persistent filesystem
Temporary cache
Skill's own root directory
Persistent Files
When your Skill needs to store some data that will persist over time and cannot easily be rebuilt, there is a persistent filesystem namespaced to your Skill.
Reading and writing to files
This uses the standard Python open() method to read and write files. It takes two parameters:
file_name (str) - a path relative to the namespace. subdirs not currently supported.
mode (str) – a file handle mode [r, r+, w, w+, rb, rb+, wb+, a, ab, a+, ab+, x]
Example:
def write_line_to_file(self, file_name, line):
"""Write a single line to a file in the Skills persistent filesystem."""
with self.file_system.open(file_name, "w") as my_file:
my_file.write(line)
def read_file(self, file_name):
"""Read the contents of a file in the Skills persistent filesystem."""
with self.file_system.open(file_name, "r") as my_file:
return my_file.read()Check if a file exists
Quick method to see if some file exists in the namespaced directory.
Example:
Get the path of the namespaced directory.
self.file_system.path is a member value containing the root path of the namespace. However it is recommended that you use the self.file_system.open() method to read and write files.
Example:
Create subdirectories
Now that we have the path of our namespaced filesystem, we can organize our files however we like within that directory.
In this example, we create a subdirectory called "cache", then write to a text file inside of it.
Example Skill
Temporary Cache
Skills can create a directory for caching temporary data to speed up performance.
This directory will likely be part of a small RAM disk and may be cleared at any time. So code that uses these cached files must be able to fallback and regenerate the file.
Example Skill
Skill Root Directory
This member variable contains the absolute path of a Skill’s root directory e.g. ~.local/share/mycroft/skills/my-skill.me/.
Generally Skills should not modify anything within this directory. Modifying anything in the Skill directory will reload the Skill. This will also prevent the Skill from updating as we do not want to overwrite changes made during development. It is also not guaranteed that the Skill will have permission to write to this directory.
Last updated
Was this helpful?