I want to be able to take a device name (eg: /dev/disk2) and determine where (if anywhere) it’s mounted (eg: /mnt/cdrom or /Volumes/RANDLABEL) in Python.
One way I can do this is to run df or mount and then parse the output, but this seems pretty cheesy and unreliable. For example, mount uses " on " as the delimiter between the device and the mountpoint. While very unlikely, either of these could potentially include that very string, making the output ambiguous.
On Linux I could read /proc/mounts, but this won’t work on Mac OS X, for example.
So I’m looking for a way to find the mountpoint for a device in a way that’s reliable (ie: can deal with arbitrary (legal) device/mountpoint names) and is “as portable as possible”. (I’m guessing that portability to Windows might not be possible — I’m not sure if it even has an analogous concept of device mountpoints.) I particularly want something that will work on both Linux and OS X.
(From my comment above:
mtabis the standard Linux way. It doesn’t exist on FreeBSD, Mac OS X or Solaris. The former two have thegetfsstat(2)andgetmntinfo(2)system calls; on Solaris you can usegetmntent(3C). Unfortunately the list of currently-mounted filesystems is not defined by POSIX AFAIK, so it is wildly different on different platforms.)There’s the experimental
mountmodule in the PSI package from PyPI, which appears to attempt to bundle all the platform-specific methods into a simple abstraction, and which is advertised as working on Mac OS X (Darwin), AIX, Linux and Solaris. The Darwin module probably works on *BSD.