View Single Post
Old 06-19-2018, 12:43 PM   #415
Doitsu
Wizard
Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.
 
Doitsu's Avatar
 
Posts: 4,315
Karma: 14277595
Join Date: Dec 2010
Device: Kindle PW2
Quote:
Originally Posted by Gergely View Post
FileNotFoundError: [Errno 2] No such file or directory: 'Text/9781118087220cover.xhtml'
You can't use open() in plugins. You'll need to use bk.readfile(id) instead. For example, the following minimal code will wrap "the" in all html files in <b> tags:

Code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys, os

# main routine    
def run(bk):
    # iterate over all html files
    for html_id, href in bk.text_iter():
        # read orignal html code from file
        original_html = bk.readfile(html_id)
        # modify html code
        modified_html = original_html.replace('the', '<b>the</b>') 
        if modified_html != original_html:
            # write modified html code to file
            bk.writefile(html_id, modified_html)
            print(os.path.basename(href) + ' updated')
    return 0

def main():
    print('I reached main when I should not have\n')
    return -1

if __name__ == "__main__":
    sys.exit(main())
Doitsu is offline   Reply With Quote