gpt4 book ai didi

Python:可用存储设备的操作系统独立列表

转载 作者:太空狗 更新时间:2023-10-29 21:06:00 25 4
gpt4 key购买 nike

有没有办法在 Python 中获取已连接存储设备的列表,例如相机、SD 卡和外部硬盘驱动器?

最佳答案

以下应该适用于 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方法很粗糙,会返回sysprocfs等驱动器,如果你想要更精细的东西,看看查询python-dbus.

关于Python:可用存储设备的操作系统独立列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12672981/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com