I dont want to use pathlib, so avoid answers with it
aString = "/test/test1/test2/test3"
aString = os.path(astring.parts[1:])
I want to get /test1/test2/test3 ,using import os or import os.path
If you can only use os
, this may suit:
my_path = '/test1/test2/test3/test4'
os.path.sep.join(['']+my_path.split(os.path.sep)[2:])
Note that it takes 2:
because the root will be in the split as well, so it just adds that back in.
Also note that if your path is actually /test1/test2/test3/test4
, then /test2/test3/test4
seems an odd value, since it still starts at the root - you may want to check if that's really what you need.