有没有一种方法可以在Python中获取已连接的存储设备列表,例如相机、SD卡和外部硬盘?
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方法非常简单,而且会返回像sys
和procfs
等驱动器,如果您需要更精细的内容,请查看使用python-dbus
查询的内容。