Finding partition containing a path?

Christian Heimes lists at cheimes.de
Sat Apr 25 00:26:43 CEST 2009


skip at pobox.com schrieb:
> This little shell script lists the partitions containing /var/opt on a set
> of hosts:
> 
>     for h in host1 host2 host3 ; do
>       echo -n "$h "
>       ssh $h df -h /var/opt | egrep -v '^Filesystem' | awk '{print $6}'
>     done
> 
> producing output like this:
> 
>     host1 /var/opt
>     host2 /var/opt
>     host3 /
> 
> Is there an easy way in Python to get the mount point in which a path
> resides?  (Unix only is fine.)

You can use os.path.ismount() to check if a path is a mount point::

import os, sys

def getmount(path):
    path == os.path.abspath(path)
    while path != "/":
        if os.path.ismount(path):
            return path
        path = os.path.abspath(os.path.join(path, os.pardir))
    return "/"

print getmount(sys.argv[1])


$ python mount.py /var/run
/var/run
$ python mount.py /var/run/cups
/var/run
$ python mount.py /var
/


Have fun!

Christian




More information about the Python-list mailing list