replace part of path - python
Question: Is there a quick way to replace part of the path in python?

for example:

    
    
    old_path='/abc/dfg/ghi/f.txt'
    

I don't know the beginning of the path (`/abc/dfg/`), so what I'd really like
to tell python to keep everything that comes after `/ghi/` (inclusive) and
replace everything before `/ghi/` with `/jkl/mno/`:

    
    
    >>> new_path
        '/jkl/mno/ghi/f.txt/'
    


Answer: 
    >>> import os.path
    >>> old_path='/abc/dfg/ghi/f.txt'
    

First grab the relative path from the starting directory of your choice using
[`os.path.relpath`](https://docs.python.org/2/library/os.path.html#os.path.relpath)

    
    
    >>> rel = os.path.relpath(old_path, '/abc/dfg/')
    >>> rel
    'ghi\\f.txt'
    

Then add the new first part of the path to this relative path using
[`os.path.join`](https://docs.python.org/2/library/os.path.html#os.path.join)

    
    
    >>> new_path = os.path.join('jkl\mno', rel)
    >>> new_path
    'jkl\\mno\\ghi\\f.txt'
    

