You could use the pathlib
module:
from pathlib import Path
pth = Path('C:/Users/arul/Desktop/jobs/project_folder/shots/shot_folder/')
print(pth)
print(pth.parent)
print(pth.parent.parent) # C:/Users/arul/Desktop/jobs/project_folder
The module has a lot more of very convenient methods for handling paths: your problem could also be solved using parts
like this:
print('/'.join(pth.parts[:-2]))
In Python 2.7 you could build your own parts
function using os.path
:
from os import path
pth = 'C:/Users/arul/Desktop/jobs/project_folder/shots/shot_folder/'
def parts(pth):
ret = []
head, tail = path.split(pth)
if tail != '':
ret.append(tail)
while head != '':
head, tail = path.split(head)
ret.append(tail)
return ret[::-1]
ret = path.join(*parts(pth)[:-2])
print(ret) # C:/Users/arul/Desktop/jobs/project_folder