2012-10-01 3 views
8

카메라, SD 카드 및 외장 하드 드라이브와 같은 연결된 저장 장치 목록을 파이썬으로 가져 오는 방법이 있습니까?파이썬 : OS 독립적 사용 가능한 저장 장치 목록

+1

어떻게 '저장 장치'를 정의합니까? 어떻게 '연결된'? –

+0

@Tichodroma Mac Finder, Windows Explorer 또는 Ubuntu 파일 브라우저에 나타나는 외부 장치 목록입니다. –

+2

'external'을 정의하십시오. – 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.... 

리눅스에 대한 위의 방법은 매우 원유이며, 당신이 뭔가 더 미세 조정, 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를 리눅스에서 읽을 수 있습니다. – LtWorf

관련 문제