[Tutor] use of webbrowser.open_new()

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Mon Aug 8 20:11:43 CEST 2005



> >     http://www.faqs.org/rfcs/rfc1738.html
> >
> > shows the syntax of file URLs.
>
> Man, that Berners-Lee can sure write plain English, can he not? I didn't
> find that reference much use at all, in truth.


Hi Tom,


[Note: when you're responding to a message on Python-tutor, please make
sure to CC to the mailing list too.]


It's meant to be a bit terse.  The second on file urls isn't too bad
though.  Here, let's take a closer look at it:

"""
   The file URL scheme is used to designate files accessible on a
   particular host computer. This scheme, unlike most other URL schemes,
   does not designate a resource that is universally accessible over the
   Internet.

   A file URL takes the form:

       file://<host>/<path>

   where <host> is the fully qualified domain name of the system on
   which the <path> is accessible, and <path> is a hierarchical
   directory path of the form <directory>/<directory>/.../<name>.

   As a special case, <host> can be the string "localhost" or the empty
   string; this is interpreted as `the machine from which the URL is
   being interpreted'.
"""


According to the file URL spec, you need to use forward slashes, not back
slashes, when defining the subdirectory path.  Your web browser might be
nice enough to try to account for backslashes, but it's not standard.
That's why the reference is relevant: it tells how to write out file urls
so that any web browser that follows the standard will do the right thing.




> webbrowser.open_new("file://C:\www\02394-Yale_Style_Manual.htm")

Here's a possible correction:

   webbrowser.open_new("file:///C:/www/02394-Yale_Style_Manual.htm")


We need three forward slashes in front of 'file:' because, in the file
URL:

       file://<host>/<path>

we're leaving the host part empty to say that we're looking at a local
file (last paragraph of the quoted section above), and forward slashes
instead of backslashes, since that's what the spec says.  *grin*



But even more importantly: backslashes in string literals are special.
You may have seen something like this before:

######
>>> print "hello\tworld\nthis is a\ntest"
hello   world
this is a
test
######


That the backslash starts an "escape character".  "\t" stands for the tab
character, and "\n" stands for the newline character.  This is relevant to
your problem because "\0239" stands for another escaped character.  For
more details on these escape sequences, see:

    http://www.python.org/doc/ref/strings.html


Quickest way to fix this: don't use backslashes!  *grin*

Use forward slashes instead.  Backslashes in string literals are special,
and we have to take special care for them.  If we do want to put a literal
backslash in our string, we have to double the backslashes up:

######
>>> s = "hello\\world"
>>> print s
hello\world
######

but it's often just simpler just to use forward slashes for paths instead.


Good luck!



More information about the Tutor mailing list