2012-10-01 13 views
8

Pythonでカメラ、SDカード、外付けハードドライブなどの接続されたストレージデバイスのリストを取得する方法はありますか?Python:利用可能なストレージデバイスのOS非依存のリスト

+1

「ストレージデバイス」はどのように定義しますか?どのように '接続'? –

+0

@Tichodroma Mac Finder、Windowsエクスプローラ、またはUbuntuファイルブラウザに表示される外部デバイスのリスト。 –

+2

'外部'を定義してください – njzk2

答えて

5

次はLinuxとWindowsで動作するはずです。 これは外部だけでなくすべてのドライブを一覧表示します!

import subprocess 
import sys 

#on windows 
#Get the fixed drives 
#wmic logicaldisk get name,description 
if 'win' in sys.platform: 
    drivelist = subprocess.Popen('wmic logicaldisk get name,description', shell=True, stdout=subprocess.PIPE) 
    drivelisto, err = drivelist.communicate() 
    driveLines = drivelisto.split('\n') 
elif 'linux' in sys.platform: 
    listdrives=subprocess.Popen('mount', shell=True, stdout=subprocess.PIPE) 
    listdrivesout, err=listdrives.communicate() 
    for idx,drive in enumerate(filter(None,listdrivesout)): 
     listdrivesout[idx]=drive.split()[2] 
# guess how it should be on mac os, similar to linux , the mount command should 
# work, but I can't verify it... 
elif 'macosx' ... 
    do the rest.... 

Linux用の上記の方法は非常に、粗である、とあなたは何かがより微調整、python-dbusで照会に見たい場合などsysprocfsのようにドライブを返します。

+1

おそらく、現在のユーザー(おそらくデスクトップにログインしているユーザー)が所有しているマウントポイントを検出するステップを追加すると、リストは「外部デバイス」のリストに非常に近くなります。プラグを抜きたいものをまずアンマウントすることができます。 – 9000

+1

ここでは、[dbus経由でUdiskを使用する方法の例](http://stackoverflow.com/a/5081937/4279)を参照してください。プロパティ[DeviceIsRemovable](http://hal.freedesktop.org/docs/udisks/Device.html#Device:DeviceIsRemovable)が近い可能性があります。 – jfs

+0

あなたは/ proc/mountsをlinuxで読むことができます。 – LtWorf

関連する問題