0
votes

I'm writing a python script that lists USB mass storage devices. I want to print out the manufacturer, the size of the device and where it's mounted. The usb mass storage devices are automatically mounted by the OS. I've got the manufacturer thing and device size working; But I'm struggling with getting the mount point. When I do df -h I get this:

FS:/dev/sda1
Size:980M
Used:0
Available:980M
used:0%
mounted on:/media/usb0

It's the mounted on, that i'm after at. This is the code I've already got:

def fetchExternals():
  bus = dbus.SystemBus()
  ud_manager_obj = bus.get_object("org.freedesktop.UDisks", "/org/freedesktop/UDisks")
  ud_manager = dbus.Interface(ud_manager_obj, 'org.freedesktop.UDisks')

  for dev in ud_manager.EnumerateDevices():
    device_obj = bus.get_object("org.freedesktop.UDisks", dev)
    device_props = dbus.Interface(device_obj, dbus.PROPERTIES_IFACE)
    if device_props.Get('org.freedesktop.UDisks.Device', "DeviceIsRemovable"):
      print device_props.Get('org.freedesktop.UDisks.Device', "DriveVendor")
      print str(math.ceil(device_props.Get('org.freedesktop.UDisks.Device', "PartitionSize")/1048576.0)/1000)+' GB'

Can somebody tell me if it's possible to get the mountpoint for the partition residing on the device?

edit: I'm new to linux and python, so sorry if this is really a noob question.

edit2: I would also like to have the available space left on the partition.

1

1 Answers

0
votes

From what I know, getting the mount points will vary according to implementation. For example, you could use diskutil on OSX, before and after a USB device is inserted to get the new device mount point. On Linux, the utilities will vary.

And, you could use this library and example to detect when a USB device is mounted. Wait for a few second for it to be mounted and check for the new devices.