Mycroft AI
  • Documentation
  • About Mycroft AI
    • Why use Mycroft AI?
    • Glossary of terms
    • Contributing
    • FAQ
  • Using Mycroft AI
    • Get Mycroft
      • Mark II
        • Mark II Dev Kit
      • Mark 1
      • Picroft
      • Linux
      • Mac OS and Windows with VirtualBox
      • Docker
      • Android
    • Pairing Your Device
    • Basic Commands
    • Installing New Skills
    • Customizations
      • Configuration Manager
      • mycroft.conf
      • Languages
        • Français (French)
        • Deutsch (German)
      • Using a Custom Wake Word
      • Speech-To-Text
      • Text-To-Speech
    • Troubleshooting
      • General Troubleshooting
      • Audio Troubleshooting
      • Wake Word Troubleshooting
      • Log Files
      • Support Skill
      • Getting more support
  • Skill Development
    • Voice User Interface Design Guidelines
      • What can a Skill do?
      • Design Process
      • Voice Assistant Personas
      • Interactions
        • Intents
        • Statements and Prompts
        • Confirmations
      • Conversations
      • Error Handling
      • Example Interaction Script
      • Prototyping
      • Design to Development
    • Development Setup
      • Python Resources
      • Your First Skill
    • Skill Structure
      • Lifecycle Methods
      • Logging
      • Skill Settings
      • Dependencies
        • Manifest.yml
        • Requirements files
      • Filesystem access
      • Skill API
    • Integration Tests
      • Test Steps
      • Scenario Outlines
      • Test Runner
      • Reviewing the Report
      • Adding Custom Steps
      • Old Test System
    • User interaction
      • Intents
        • Padatious Intents
        • Adapt Intents
      • Statements
      • Prompts
      • Parsing Utterances
      • Confirmations
      • Conversational Context
      • Converse
    • Displaying information
      • GUI Framework
      • Show Simple Content
      • Mycroft-GUI on a PC
      • Mark 1 Display
    • Advanced Skill Types
      • Fallback Skill
      • Common Play Framework
      • Common Query Framework
      • Common IoT Framework
    • Mycroft Skills Manager
      • Troubleshooting
    • Marketplace Submission
      • Skills Acceptance Process
        • Information Review Template
        • Code Review Template
        • Functional Review Template
        • Combined Template
      • Skill README.md
    • FAQ
  • Mycroft Technologies
    • Technology Overview
    • Roadmap
    • Mycroft Core
      • MessageBus
      • Message Types
      • Services
        • Enclosure
        • Voice Service
        • Audio Service
        • Skills Service
      • Plugins
        • Audioservice Plugins
        • STT Plugins
        • TTS Plugins
        • Wake Word Plugins
      • Testing
      • Legacy Repo
    • Adapt
      • Adapt Examples
      • Adapt Tutorial
    • Lingua Franca
    • Mimic TTS
      • Mimic 3
      • Mimic 2
      • Mimic 1
      • Mimic Recording Studio
    • Mycroft GUI
      • Remote STT and TTS
    • Mycroft Skills Kit
    • Mycroft Skills Manager
    • Padatious
    • Precise
    • Platforms
Powered by GitBook
On this page
  • Persistent Files
  • Reading and writing to files
  • Check if a file exists
  • Get the path of the namespaced directory.
  • Create subdirectories
  • Example Skill
  • Temporary Cache
  • Example Skill
  • Skill Root Directory

Was this helpful?

  1. Skill Development
  2. Skill Structure

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:

        file_name = "example.txt"
        with self.file_system.open(file_name, "w") as my_file:
            my_file.write("Hello world")
        self.log.info(self.file_system.exists(file_name))
        # True
        self.log.info(self.file_system.exists("new.txt"))
        # False

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:

from mycroft import MycroftSkill

class FileSystemSkill(MycroftSkill):

    def initialize(self):
        """Log the path of this Skills persistent namespace."""
        self.log.info(self.file_system.path)

def create_skill():
    return FileSystemSkill()

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.

from os import mkdir
from os.path import join

from mycroft import MycroftSkill

class FileSystemSkill(MycroftSkill):

    def initialize(self):
        """Create a cache subdirectory and write to a file inside it"""
        cache_dir = "cache"
        file_name = "example.txt"
        if not self.file_system.exists(cache_dir):
            mkdir(join(self.file_system.path, cache_dir))
        with self.file_system.open(join(cache_dir, file_name), "w") as my_file:
            my_file.write('hello')


def create_skill():
    return FileSystemSkill()

Example Skill

from mycroft import MycroftSkill, intent_handler

class FileSystemSkill(MycroftSkill):

    def initialize(self):
        """Perform initial setup for the Skill.

        For this example we do four things:
        1. Log the path of this directory.
        2. Write to a file in the directory.
        3. Check that our file exists.
        4. Read the contents of our file from disk.
        """
        file_name = "example.txt"
        self.log.info(self.file_system.path)
        self.write_line_to_file(file_name, "hello world")
        self.log.info(self.file_system.exists(file_name))
        self.log.info(self.read_file(file_name))

    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()

def create_skill():
    return FileSystemSkill()

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

from os.path import join
from mycroft import MycroftSkill, intent_handler
from mycroft.util import get_cache_directory

class CachingSkill(MycroftSkill):

    def initialize(self):
        """Perform initial setup for the Skill.

        For this example we do four things:
        1. Get a cache directory namespaced for our Skill.
        2. Define a file path for the cache_file.
        3. Write some data to the cache_file
        4. Log the path of the cache_file
        4. Log the contents of the cache_file.
        """
        cache_dir = get_cache_directory('CachingSkill')
        self.cache_file = join(cache_dir, "myfile.txt")
        self.cache_data()
        self.log.info(self.cache_file)
        self.log.info(self.read_cached_data())

    def cache_data(self):
        with open(self.cache_file, "w") as cache_file: 
            cache_file.write("Some cached data") 

    def read_cached_data(self):
        with open(self.cache_file, "r") as cache_file: 
            return cache_file.read()

def create_skill():
    return CachingSkill()

Skill Root Directory

self.root_dir

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.

PreviousRequirements filesNextSkill API

Last updated 4 years ago

Was this helpful?