python - how can the directory of a usb drive connected to a system be obtained? -
i need obtain path directory created usb drive(i think it's /media/user/xxxxx) simple usb mass storage device browser making. can suggest best/simplest way this? using ubuntu 13.10 machine , using on linux device.
need in python.
this should started:
#!/usr/bin/env python import os glob import glob subprocess import check_output, calledprocesserror def get_usb_devices(): sdb_devices = map(os.path.realpath, glob('/sys/block/sd*')) usb_devices = (dev dev in sdb_devices if 'usb' in dev.split('/')[5]) return dict((os.path.basename(dev), dev) dev in usb_devices) def get_mount_points(devices=none): devices = devices or get_usb_devices() # if devices none: get_usb_devices output = check_output(['mount']).splitlines() is_usb = lambda path: any(dev in path dev in devices) usb_info = (line line in output if is_usb(line.split()[0])) return [(info.split()[0], info.split()[2]) info in usb_info] if __name__ == '__main__': print get_mount_points()
how work?
first, parse /sys/block
sd*
files (courtesy of https://stackoverflow.com/a/3881817/1388392) filter out usb devices. later call mount
, parse output lines devices.
of course might edge cases, when won't work, portability issues etc. or better ways it. more information should rather seek on superuser or serverfault, more experienced linux hackers.
Comments
Post a Comment