有没有办法在 Python 中获取已连接的存储设备列表,如相机、SD 卡和外部硬盘驱动器?
问问题
6728 次
1 回答
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 的上述方法非常粗糙,并且会返回诸如此类的驱动器sys
,procfs
如果您想要更精细的调整,请查看使用python-dbus
.
于 2012-10-01T12:48:12.573 回答