0

I'm trying to familiarize myself with Python with a major concern for web publishing, so I looked around and found the following example. Running 2.7 in PyScripter on Windows 7 didn't bring up the browser as I expected. The code came up in Notepad++ instead, apparently since the html suffix was associated with Notepad. I tried about a dozen different permutations of the code, but the html file still opened in Notepad until I associated that file with Firefox. When I include the print webbrowser._browsers command, I get {'windows-default': [, None], 'c:\program files (x86)\internet explorer\iexplore.exe': [None, ]}

This would imply to me that IE should be the default browser being called, but obviously it is not. Can anyone enlighten me here as I am a Python newbie?

'''A simple program to create an html file froma given string,
and call the default web browser to display the file.'''

contents = '''<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta content="text/html; charset=ISO-8859-1"
 http-equiv="content-type">
  <title>Hello</title>
</head>
<body>
Hello, World!
</body>
</html>
'''

import webbrowser

def main():
    browseLocal(contents)

def strToFile(text, filename):
    """Write a file with the given name and the given text."""
    output = open(filename,"w")
    output.write(text)
    output.close()

def browseLocal(webpageText, filename='C:\\Python27\\Programs\\tempBrowseLocal.html'):
    '''Start your webbrowser on a local file containing the text
    with given filename.'''
    strToFile(webpageText, filename)
    print webbrowser._browsers
    webbrowser.open(filename)


main()
bobv
  • 41
  • 7

1 Answers1

0
def browseLocal(webpageText):#take filename out of here
    '''Start your webbrowser on a local file containing the text
    with given filename.'''
    #define filename here.
    filename='C:\\Python27\\Programs\\tempBrowseLocal.html'
    strToFile(webpageText, filename)
    print webbrowser._browsers
    webbrowser.open(filename)
mcmxl
  • 93
  • 1
  • 2
  • 8
  • Moving the filename declaration hasn't worked. If the html file is associated with notepad, then notepad comes up, not the default browser – bobv Dec 04 '16 at 18:58
  • @Bobv it may be that the webbrowser module does not open local files. Try the sub process.popen() as suggested here http://stackoverflow.com/questions/15054434/how-can-i-open-files-in-external-programs-in-python – mcmxl Dec 04 '16 at 19:19