Python:独立于操作系统的可用存储设备列表 [英] Python: OS Independent list of available storage devices

查看:86
本文介绍了Python:独立于操作系统的可用存储设备列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以用Python获取连接的存储设备列表,例如相机,SD卡和外部硬盘驱动器?

Is there a way go get a list of connected storage devices, like Cameras, SD cards and external Hard Drives, in Python?

推荐答案

以下内容适用于Linux和Windows。
这将列出所有驱动器,而不仅仅是外部驱动器!

The following should work for Linux and Windows. This will list ALL drives, not just externals!

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的方法非常粗略,将恢复rn驱动器,例如 sys procfs 等,如果您希望进行更精细的调整,请使用<$ c进行查询$ c> python-dbus 。

The above method for Linux is very crude, and will return drives like sys and procfs etc., if you want something more fine tuned, look into querying with python-dbus.

这篇关于Python:独立于操作系统的可用存储设备列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆