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

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -