如何获取插入计算机的可移动驱动器列表? [英] How can I get a list of removable drives plugged in the computer?

查看:33
本文介绍了如何获取插入计算机的可移动驱动器列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取插入计算机的可移动驱动程序列表.

我认为可以通过使用一些注册表来完成,但我不知道具体如何.

如果有其他方式我想听听.

注意:重要的是我将能够将可移动驱动器与固定驱动器分开.

解决方案

算法很简单:

这就是它在 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:

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屋!

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