利用可能な USB デバイスを一覧表示する小さなプログラムを作成しています。他のスクリプトからいつでもアクセスできるように、関数ごとに別のファイルを作成しました。この関数の主な目的は、リスト/配列を出力として返すことです。print コマンドを使用すると、使用可能なデバイスのリストが正常に出力されます。ただし、return コマンドを使用すると、最初に検出されたデバイスのみが返されます。私は SO から他の同様の質問を受けましたが、この問題に対する有効な解決策が見つかりませんでした。dbus を使用して試したコードを次に示します。どんな助けでも感謝します。
#!/usr/bin/python2.7
import dbus
def find_usb(self):
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', "DriveConnectionInterface") == "usb" and device_props.Get('org.freedesktop.UDisks.Device', "DeviceIsPartition"):
if device_props.Get('org.freedesktop.UDisks.Device', "DeviceIsMounted"):
device_file = device_props.Get('org.freedesktop.UDisks.Device', "DeviceFile")
#print device_file
return device_file
else:
print "Device not mounted"
find_usb("")