backslash woes........
Martin Franklin
martin.franklin at westerngeco.com
Tue Jul 10 10:08:33 EDT 2001
gbreed at cix.compulink.co.uk wrote:
>
> In article <Xns90DA75C5FEBC9duncanrcpcouk at 127.0.0.1>,
> duncan at NOSPAMrcp.co.uk (Duncan Booth) wrote:
>
> > "Duncan Booth" <duncan at NOSPAMrcp.co.uk> wrote in
> > news:Xns90DA74BD3B968duncanrcpcouk at 127.0.0.1:
> >
> > > Martin Franklin <martin.franklin at westerngeco.com> wrote in
> > > news:3B4AC7C2.DD53F6BB at westerngeco.com:
> > >
> > >> I am having trouble with windows path names (i'm a UNIX'ite)
> >
> > Another useful tip to remember is that except for the DOS command
> > prompt, and some badly behaved applications, Windows actually supports
> > both forward slash and backslash as path separators. So in a Python
> > program (or most other languages) you can use forward slashes anywhere
> > you want to write a pathname.
>
> So long as you aren't using data given to you by the OS. In this case, I
> think the duality is more of a problem than a solution. Here's a function
> that I think solves the poster's original problem:
>
> def getPrefixRelatives(*filenames):
> """return the common prefix of the filenames
> and each filename relative to that prefix
>
> """
> # make sure all paths are spelt the same
> normalized = map(os.path.normpath, filenames)
> prefix = os.path.commonprefix(normalized)
> return(prefix, [
> filename[len(prefix):] for filename in normalized])
>
> >>> import os
> >>> getPrefixRelatives('c:/python/first.py', 'c:\\python\\sub\\second.py')
> ('c:\\python\\', ['first.py', 'sub\\second.py'])
>
> Returning UNIX-style / separated pathnames is left as an exercise for the
> reader.
>
> Graham
Bingo! Thanks for this (and all the other sugestions....ofcourse!) It didn't
occur to me to use slicing to remove the prefix (simplest is best!)
More information about the Python-list
mailing list