[Tutor] use of webbrowser.open_new()

Kent Johnson kent37 at tds.net
Mon Aug 8 13:08:32 CEST 2005


Tom Cloyd wrote:
> Having just discovered the webbrowser module, I've confirmed that I can  
> indeed open a URL in my default browser using something like
> 
> webbrowser.get_new("{URL}")
> 
> What I want to do is open a web page stored on my local machine. I'm not  
> getting it to work -
> 
> webbrowser.open_new("C:\__Library\folders\05238 Design for Confusion\05238  
> Design for Confusion -05krugman.html")

The backslash (\) has a special meaning in strings; it means, "interpret the next character as a special code". A complete list of the codes is here:
http://docs.python.org/ref/strings.html

So \f means FORM FEED and \052 means the character with octal value 052 which happens to be an asterisk.

That is why the file name in the error message looks different from the one you wrote.

There are two ways to fix this. First, you can prefix the string with an 'r' (for 'raw') which disables most of the \ escapes. This is handy when you are copying the file name from Windows Explorer:
webbrowser.open_new(r"C:\__Library\folders\05238 Design for Confusion\05238 Design for Confusion -05krugman.html")

Alternately you can use / instead of \; this will work fine:
webbrowser.open_new("C:/__Library/folders/05238 Design for Confusion/05238 Design for Confusion -05krugman.html")

Kent

> Traceback (most recent call last):
>    File "C:\Program Files\ActiveState Komodo  
> 3.1\dbgp\pythonlib\dbgp\client.py", line 3149, in runcode
>      locals = self.frame.f_locals)
>    File "C:\Program Files\ActiveState Komodo  
> 3.1\dbgp\pythonlib\dbgp\client.py", line 1569, in runcode
>      h_exec(code, globals=globals, locals=locals, module=module)
>    File "C:\Program Files\ActiveState Komodo  
> 3.1\dbgp\pythonlib\dbgp\client.py", line 516, in __init__
>      exec code in globals, locals
>    File "<console>", line 0, in __main__
>    File "C:\Python24\Lib\webbrowser.py", line 46, in open_new
>      get().open(url, 1)
>    File "C:\Python24\Lib\webbrowser.py", line 250, in open
>      os.startfile(url)
> WindowsError: [Errno 2] The system cannot find the file specified:  
> 'C:\\__Library\x0colders*38 Design for Confusion*38 Design for Confusion  
> -05krugman.html'
> 
> Can you suggest what I might be doing wrong here?
> 
> -- t.
> 
> ======================================================
> Tom Cloyd
> Bellingham, Washington, U.S.A: (360) 920-1226
> << BestMindHealth.com / tc at bestmindhealth.com >>
> ======================================================
> 
> Using Opera's revolutionary e-mail client (program):  
> http://www.opera.com/mail/
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 



More information about the Tutor mailing list