How do I match the last occurence of a character?

Blake Winton bwinton at tor.dhs.org
Thu Jun 10 00:38:01 EDT 1999


> I'm pretty sure that it does basically the same thing your code did...
> just under the covers.  I haven't used it on NT, but I know on unix it
> handles everything gracefully:

One of the nice things about Unices is that a whole lot of specs are
written to accomodate them.  Like using "/" for the separator in URLs.
I hypothesize that if the Mr. Berners-Lee was a Mac addict, the Weeb
would look a lot different today.

> It just splits off the last part and calls that the filename and the
> rest is the basename.

The Macintosh version of os.split (which is accessed as os.split, and
can be grabbed from macpath.py), looks like:

# Split a pathname in two parts: the directory leading up to the final
# bit, and the basename (the filename, without colons, in that
# directory).  The result (s, t) is such that join(s, t) yields the
# original argument.
def split(s):
        if ':' not in s: return '', s
        colon = 0
        for i in range(len(s)):
                if s[i] == ':': colon = i+1
        path, file = s[:colon-1], s[colon:]
        if path and not ':' in path:
                path = path + ':'
        return path, file

And when we use it to split, we get results like

>>> name = "http://www.foo.com/~bwinton/test.html"
>>> name2 = "/home/bwinton/test.html"
>>> split( name )
('http:', '//www.foo.com/~bwinton/test.html')
>>> split( name2 )
('', '/home/bwinton/test.html')

Which I think you will agree is not the desired behaviour.
(ntpath.split seems to do the right thing, though.)

Later,
Blake.





More information about the Python-list mailing list