Python:OS独立的可用存储设备列表

Ada*_*tan 8 python cross-platform

有没有办法在Python中获得连接存储设备的列表,如相机,SD卡和外置硬盘?

Oz1*_*123 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....
Run Code Online (Sandbox Code Playgroud)

适用于Linux上面的方法是非常粗糙的,并且将返回驱动器像sysprocfs等,如果你想要更多的东西微调,外观与查询python-dbus.