Replace funky '/'.join(f.split('/')[:-1]) hack with the equivalent posixpath.dirname

(which will work correctly regardless of host platform)

This makes the code easier to follow, since that join/split isn't obviously just walking up the tree (functionally it is just walking the tree, but it's definitely not easy on the eyes).