如何获取插入计算机的可移动驱动器列表? [英] How can I get a list of removable drives plugged in the computer?
问题描述
我想获取插入计算机的可移动驱动程序列表.
我认为可以通过使用一些注册表来完成,但我不知道具体如何.
如果有其他方式我想听听.
注意:重要的是我将能够将可移动驱动器与固定驱动器分开.
算法很简单:
- 调用 [MS.Docs]:GetGetLogicalDriveStringsW 函数,它将返回一个包含所有现有rootdirs(例如C:\\)的字符串,由NULL(em>\x00) 字符s
- 迭代 rootdirs 并使用 [MS.Docs]:GetDriveTypeW 函数
- 过滤可移动驱动器(类型为 DRIVE_REMOVABLE)
这就是它在 Python 中的样子(使用 PyWin32 包装器).将任何 win32con.DRIVE_*
常量添加到 drive_types 元组以获得不同的驱动器类型组合:
code00.py:
#!/usr/bin/env python3导入系统导入 win32con从 win32api 导入 GetLogicalDriveStrings从 win32file 导入 GetDriveTypedef get_drives_list(drive_types=(win32con.DRIVE_REMOVABLE,)):drive_str = GetLogicalDriveStrings()驱动器 = [drives_str.split("\x00") 中的项目的项目如果项目]如果 drive_types 为 None 或 Drive_types 中的 GetDriveType(item),则为驱动器中的项目返回 [item[:2]]定义主():drive_filters_examples = [(无,全部"),((win32con.DRIVE_REMOVABLE,), "可移动"),((win32con.DRIVE_FIXED, win32con.DRIVE_CDROM), "Fixed and CDROM"),]for (drive_types_tuple, display_text) in drive_filters_examples:驱动器 = get_drives_list(drive_types=drive_types_tuple)打印({0:s} 个驱动器:".format(display_text))对于驱动器驱动器:打印({0:s}".格式(驱动器),结束=")打印(\n")如果 __name__ == "__main__":print("Python {0:s} {1:d}bit on {2:s}\n".format(" ".join(item.strip() for item in sys.version.split("\n")), 64 如果 sys.maxsize > 0x100000000 否则 32, sys.platform))主要的()打印(\n完成.")
输出:
<块引用>[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q041465580]>"e:\Work\Dev\VEnvs\py_064_03.08.00_test0\Scripts\python.exe" code00.pyPython 3.8.0 (tags/v3.8.0:fa919fd, Oct 14 2019, 19:37:50) [MSC v.1916 64 bit (AMD64)] 64bit on win32所有驱动器:C: D: E: F: G: H:可移动驱动器:乙:固定和 CDROM 驱动器:C: E: F: G: H:完毕.
I would like to get a list of the removable drivers that are plugged in to the computer.
I think it can be done by using some registry, but I don't know how exactly.
If there is another way I would like to hear about it.
Note: It's important that I will be able to separate the removable drives from the fixed drives.
The algorithm is straightforward:
- Call [MS.Docs]: GetGetLogicalDriveStringsW function, which will return a string containing all the existing rootdirs (e.g. C:\\) separated by NULL (\x00) chars
- Iterate over the rootdirs and get each one's type using [MS.Docs]: GetDriveTypeW function
- Filter the removable drives (having the type DRIVE_REMOVABLE)
This is how it looks in Python (using PyWin32 wrappers). Add any of win32con.DRIVE_*
constants to drive_types tuple to get different drive types combinations:
code00.py:
#!/usr/bin/env python3
import sys
import win32con
from win32api import GetLogicalDriveStrings
from win32file import GetDriveType
def get_drives_list(drive_types=(win32con.DRIVE_REMOVABLE,)):
drives_str = GetLogicalDriveStrings()
drives = [item for item in drives_str.split("\x00") if item]
return [item[:2] for item in drives if drive_types is None or GetDriveType(item) in drive_types]
def main():
drive_filters_examples = [
(None, "All"),
((win32con.DRIVE_REMOVABLE,), "Removable"),
((win32con.DRIVE_FIXED, win32con.DRIVE_CDROM), "Fixed and CDROM"),
]
for (drive_types_tuple, display_text) in drive_filters_examples:
drives = get_drives_list(drive_types=drive_types_tuple)
print("{0:s} drives:".format(display_text))
for drive in drives:
print("{0:s} ".format(drive), end="")
print("\n")
if __name__ == "__main__":
print("Python {0:s} {1:d}bit on {2:s}\n".format(" ".join(item.strip() for item in sys.version.split("\n")), 64 if sys.maxsize > 0x100000000 else 32, sys.platform))
main()
print("\nDone.")
Output:
[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q041465580]> "e:\Work\Dev\VEnvs\py_064_03.08.00_test0\Scripts\python.exe" code00.py Python 3.8.0 (tags/v3.8.0:fa919fd, Oct 14 2019, 19:37:50) [MSC v.1916 64 bit (AMD64)] 64bit on win32 All drives: C: D: E: F: G: H: Removable drives: D: Fixed and CDROM drives: C: E: F: G: H: Done.
这篇关于如何获取插入计算机的可移动驱动器列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!