python - Path manipulation -
consider launching script in following linux path:
/home/chris/sources/a/b/c/d/e/test.py
i want able check whether folder named sources in path , return path /home/chris/sources/
do think should using simple string manipulation, or have in python xpath library can me it?
inb4padraic
path = '/home/chris/sources/a/b/c/d/e/test.py'.split('/') if 'sources' in path: print '/'.join(path[:path.index('sources') + 1]) another way
import os path = '/home/chris/sources/a/b/c/d/e/test.py'.split('sources') if len(path) > 1: print os.path.join(path[0], 'sources')
Comments
Post a Comment