Mycroft Community Forum

Reading and writing to files

Hello everyone,

I’m trying to read or write to a text file(in the same skill directory) and for some reason. My code is not doing that. I have created a simple script to execute the reading file function and it does not work… Please find below my test script.

#******************* Mycroft #
from adapt.intent import IntentBuilder
from mycroft.skills.core import MycroftSkill, intent_handler
from mycroft.util.log import LOG, getLogger
from mycroft.filesystem import FileSystemAccess
#
****************************#

#******************* Mycroft ************************#

class ReadtextSkill(MycroftSkill):

# The constructor of the skill, which calls MycroftSkill's constructor
def __init__(self):
    super(ReadtextSkill, self).__init__(name="ReadtextSkill")

@intent_handler(IntentBuilder("ReadtextIntent").require("Run").require("Readtext"))
def handle_readtext_intent(self, message):

    self.speak_dialog("running")

#****************************************************#
try:
data = “”
filename = “example.txt”
with self.file_system.open(filename, “r”) as my_file:
data = my_file.read()

        self.speak_dialog('result', {'TEXT': data})
        #****************************************************#
    except:
        self.speak_dialog("error")

def shutdown(self):
    super(ReadtextSkill, self).shutdown()

def stop(self):
    pass

def create_skill():
return ReadtextSkill()

#****************************************#

I have checked all the links available in the forum and I have not been able to fix it…

Any ideas ?

Thanks in advance

https://mycroft-ai.gitbook.io/docs/skill-development/skill-structure/filesystem

I have found the problem. I thought that the files were save on the same skills folder (/opt/mycroft/skills/readtext) but no.

Mycroft creates a different folder to save all the files generated using the skills. in my case was the following:

~/.mycroft/skills/ReadtextSkill

I hope this helps, if you’re facing the same issue.

Cheers!

1 Like

FYI, there is, in fact, a clear reason for this apparent oddity: Mycroft’s skill updater will skip any skills which have been modified locally, because it assumes the user is hacking on those skills. If your skill wrote data to its own folder, it would almost certainly prevent itself from updating.

This might be avoidable if skill devs could be taught to .gitignore all the file extensions or filenames they intend to write, but that’s a tall order on both levels.

1 Like

Hi lenninic - welcome to Mycroft :slight_smile:

Glad you worked it out too.

To extend on what Chance said, it also means that Skills don’t need to be in a writeable location, and means that user content is stored in the users home directory (soon to respect the XDG standard)