views:

345

answers:

4

Hello,

let's say i have directory paths looking like this:

this/is/the/basedir/path/a/include
this/is/the/basedir/path/b/include
this/is/the/basedir/path/a
this/is/the/basedir/path/b

In Python, how can i split these paths up so they will look like this instead:

a/include
b/include
a
b

If i run os.path.split(path)[1] it will display:

include
include
a
b

What should i be trying out here, should i be looking at some regex command or can this be done without it? Thanks in advance.

EDIT ALL: I solved it using regular expressions, damn handy tool :)

+1  A: 

what about partition?
It Split the string at the first occurrence of sep, and return a 3-tuple containing the part before the separator, the separator itself, and the part after the separator. If the separator is not found, return a 3-tuple containing the string itself, followed by two empty strings.

data = """this/is/the/basedir/path/a/include
this/is/the/basedir/path/b/include
this/is/the/basedir/path/a
this/is/the/basedir/path/b"""
for line in data.splitlines():
    print line.partition("this/is/the/basedir/path/")[2]

#output
a/include
b/include
a
b

Updated for the new comment by author:
It looks like u need rsplit for different directories by whether the directory endswith "include" of not:

import os.path
data = """this/is/the/basedir/path/a/include
this/is/the/basedir/path/b/include
this/is/the/basedir/path/a
this/is/the/basedir/path/b"""
for line in data.splitlines():
    if line.endswith('include'):
        print '/'.join(line.rsplit("/",2)[-2:])
    else:
        print os.path.split(line)[1]
        #or just
        # print line.rsplit("/",1)[-1]
#output
a/include
b/include
a
b
sunqiang
+3  A: 

Perhaps something like this, depends on how hardcoded your prefix is:

def removePrefix(path, prefix):
    plist = path.split(os.sep)
    pflist = prefix.split(os.sep)
    rest = plist[len(pflist):]
    return os.path.join(*rest)

Usage:

print removePrefix("this/is/the/basedir/path/b/include", "this/is/the/basedir/path")
b/include

Assuming you're on a platform where the directory separator (os.sep) really is the forward slash).

This code tries to handle paths as something a little more high-level than mere strings. It's not optimal though, you could (or should) do more cleaning and canonicalization to be safer.

unwind
+1  A: 

Maybe something like this:

result = []

prefix = os.path.commonprefix(list_of_paths)
for path in list_of_paths:
    result.append(os.path.relpath(path, prefix))

This works only in 2.6. The relapath in 2.5 and before does the work only in case the path is the current working directory.

mp
A: 

While the criterion is not 100% clear, it seems from the OP's comment that the key issue is specifically whether the path's last component ends in "include". If that is the case, and to avoid going wrong when the last component is e.g. "dontinclude" (as another answer does by trying string matching instead of path matching), I suggest:

def lastpart(apath):
    pieces = os.path.split(apath)
    final = -1
    if pieces[-1] == 'include':
        final = -2
    return '/'.join(pieces[final:])
Alex Martelli