Search code examples
pythonpython-3.xunixpathvolumes

Find root of mapped volume (network-attached storage)


I have a network-attached storage mapped to /Volumes/Media, and then I use the path

/Volumes/Media/some/path/

in my program.

I want to be able to determine the what part of the path is the mapped drive, and which is the rest. Something like

os.path.split_volume('/Volumes/Media/some/path/') == '/Volumes/Media', 'some/path'

I am currently on Mac OS X, but I image that the code will generally run in a Linux environment.


Solution

  • I think I found the answer here: https://stackoverflow.com/a/4453715/4237316

    def split_on_mount_point(path):
        mount_point = os.path.abspath(path)
        while not os.path.ismount(mount_point):
            mount_point = os.path.dirname(mount_point)
    
        return mount_point, os.path.relpath(path, mount_point)