Output to screen and html file, execute text file

Updated TrustyTony 0 Tallied Votes 309 Views Share

Here is code to execute these printing commands from file:

talk("How Are You")
bark("I am fine")
bark("...and how are you")
snarl("...sorry I asked")

from file INPUT, do printing of results to screen without tags and to file with tags, and open the html output with webbrowser.

from __future__ import print_function

import os

import webbrowser

OUTPUT, INPUT = 'output.html', 'talk.txt'

def print_and_save(*args, **kwargs):
    """ Echo args to output.html with tags and console without them """
    if 'tag' in kwargs:
       tag = kwargs['tag']
       del kwargs['tag']
       print(tag + ' '.join(args) + tag[0] + '/' + tag[1:] + '</br>',
             file=open(OUTPUT, 'a'),
             **kwargs)
    else:
         print(' '.join(args)+'</br>', file=open(OUTPUT, 'a'), **kwargs)

    print(' '.join(args), **kwargs)

def talk(arg):
    print_and_save(arg)

def bark(arg):
    print_and_save(arg, tag='<b>')

def snarl(arg):
    print_and_save(arg, tag='<i>')

def main():
    print('<html><body>', file=open(OUTPUT, 'w'))
    exec(open(INPUT).read())
    print('\n</body> </html> ', file=open(OUTPUT, 'a'))

main()
webbrowser.open(OUTPUT)