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 🙂
Perhaps something like this, depends on how hardcoded your prefix is:
Usage:
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.