I am stuck on a python print read problem - maybe someone is kind enough to help

Hi there, I’m stuck and probably relatively simple python action. but for the life of me I can not figure it out. it for a skill to read ebooks… it works fine on that part but for the section to continue reading ebooks I am stuck on . basically I just wanted to write a dat file with a name of the book being read and a line count file that keeps tracks of the last line read.

i can write the the book path no problem. and read it but can not input it as a variable.

and the count i can neither write or import as a variable.

i want hard copy of the data then i continue listening to a book from where i last left off at after a power outage.

well here basically where I am stuck

#################

@intent_file_handler('continue.read.intent')
def handle_continue_read(self, message):
   self.is_reading = True
   self.speak(' one moment ')
   
   #filepath = '/opt/mycroft/skills/gutenberg-library-skill/ebook/little_women' ### works as direct link
   #cnt = 6  ## works fine
   
   filepath = open("book.dat","r")  ##   TypeError: expected str, bytes or os.PathLike object, not _io.TextIOWrapper  -- when inputting to -- with open(filepath) as f: 

   cnt = open("cnt.dat","r")  ## out puts error TypeError: '_io.TextIOWrapper' object cannot be interpreted as an integer   --- when inputting into --  for i in range(cnt):

   with open(filepath) as f: 
      
       for i in range(cnt):
           f.readline()          
       for  line in (f):
              
              if self.is_reading is False:
                  #print ('stopping  book')
                  break
              else:
                  #print ('reading book')
                  wait_while_speaking()              
                  self.speak("{}".format(line))
                  cnt = cnt+1
                  file1 = open("cnt.dat","w")   ##  file1.write(cnt)  ---TypeError: write() argument must be str, not int  ---- can not write file
                  file1.write(cnt)
                  
   if self.is_reading is True:
      self.is_reading = False

okay figured it out

i did not use filepath.read() to actually read it
and to convert between str and int use str() for str and int() for integer

if someone wants I probably up load my gutenberg skill today sometimes… it downloads ebooks from gutenberg. processes the files into a useable document for mycroft, it works well on older gutenberg ebooks of the classics… the newer books where they use OCR. then you have to edit the book a bit to remove content page… and other artifacts from OCRs but that usually takes less then a minute to do to make usable

2 Likes

Hey glad you worked it out and sounds like a cool Skill.

What process do you use to edit the book? It could probably be automated if you wanted.

Edit: Nevermind, just saw your other post, congratulations on a great Skill!

thank you hope it works well for people, i probably add in jump to line or go to chapter. but i think that probably mean making a companion file or append the end of the book file with the chapter info… while i got you is there a way to prevent attempting to load skill for a short period, as when my download app is processing files . each time it makes an amendment to the file it… mycroft attempt to reload it… it be nice if i can stop that until it finished processing … because it can take 20 minutes before mycroft catches up to all the amendments… and it can be used again… where as if i could stop that . then there would only be one retraining processes instead of hundreds in a row

It would be worth using self.file_system.path instead of a hardcoded filepath.

This provides a dedicated directory (~/.mycroft/skills/YourSkillName) that is consistent across installs. As it’s outside of the Skill directory it won’t reload anytime something changes.

okay thank you i keep that in mind- but i think the problem with that is my bash script the does the processing of websites works outside that mycroft… possibly i could use self.file_system.path can used as variable and i pass it on to the script

is there an example usage somewhere - i looked through mycroft documents does not seam to even mention it …

maybe i will just add a line at the beginning of the script cp init.py tmp.init and then cp tmp.init init.py back when it finishes…

The AIML Fallback Skill uses it