how can the directory of a usb drive connected to a system be obtained?

我只是一个虾纸丫 提交于 2020-01-12 08:24:08

问题


I need to obtain the path to the directory created for a usb drive(I think it's something like /media/user/xxxxx) for a simple usb mass storage device browser that I am making. Can anyone suggest the best/simplest way to do this? I am using an Ubuntu 13.10 machine and will be using it on a linux device.

Need this in python.


回答1:


This should get you started:

#!/usr/bin/env python

import os
from glob import glob
from subprocess import check_output, CalledProcessError

def get_usb_devices():
    sdb_devices = map(os.path.realpath, glob('/sys/block/sd*'))
    usb_devices = (dev for dev in sdb_devices
        if 'usb' in dev.split('/')[5])
    return dict((os.path.basename(dev), dev) for dev in usb_devices)

def get_mount_points(devices=None):
    devices = devices or get_usb_devices() # if devices are None: get_usb_devices
    output = check_output(['mount']).splitlines()
    is_usb = lambda path: any(dev in path for dev in devices)
    usb_info = (line for line in output if is_usb(line.split()[0]))
    return [(info.split()[0], info.split()[2]) for info in usb_info]

if __name__ == '__main__':
    print get_mount_points()

How does it work?

First, we parse /sys/block for sd* files (courtesy of https://stackoverflow.com/a/3881817/1388392) to filter out usb devices. Later you call mount and parse output for lines only for those devices.

Of course they might be some edge cases, when this won't work, portability issues etc. Or better ways to do it. But for more information you should rather seek help on SuperUser or ServerFault, with more experienced linux hackers.




回答2:


Using m.wasowski code, unexpected behavior can occur:

return [(info.split()[0], info.split()[2]) for info in usb_info]

This part of code can produce bug, if your USB device name has white space character in it. I got that behavior with device named "USB DEVICE".

info.split()[2]

Returned media/home/USB for me, when it is media/home/USB DEVICE.

I modified that part, so it was founding word "type", and replaced that line with this:

#return [(info.split()[0], info.split()[2]) for info in usb_info]

fullInfo = []
for info in usb_info:
    print(info)
    mountURI = info.split()[0]
    usbURI = info.split()[2]
    print(info.split().__sizeof__())
    for x in range(3, info.split().__sizeof__()):
        if info.split()[x].__eq__("type"):
            for m in range(3, x):
                usbURI += " "+info.split()[m]
            break
    fullInfo.append([mountURI, usbURI])
return fullInfo



回答3:


I had to modify @m.wasowski 's code to make it work on Python3.5.4 as follows.

def get_mount_points(devices=None):
    devices = devices or get_usb_devices()  # if devices are None: get_usb_devices
    output = check_output(['mount']).splitlines()
    output = [tmp.decode('UTF-8') for tmp in output]

    def is_usb(path):
        return any(dev in path for dev in devices)
    usb_info = (line for line in output if is_usb(line.split()[0]))
    return [(info.split()[0], info.split()[2]) for info in usb_info]


来源:https://stackoverflow.com/questions/22615750/how-can-the-directory-of-a-usb-drive-connected-to-a-system-be-obtained

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!