I came here looking for something very similar. My solution, based on the above by @larsks, and assuming you want to preserve the entire path except the filename, is to do:
>>> import pathlib
>>> p = pathlib.Path('/path/to/my/file')
>>> pathlib.Path('/'.join(list(p.parts)[1:-1])+'/')
Essentially, list(p.parts)[1:-1] creates a list of Path elements, starting from the second to n-1th, and you join them with a '/' and make a path of the resulting string. Edit The final +'/' adds in the trailing slash - adjust as required.