Python 如何获取插入计算机的可移动驱动器列表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41465580/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 01:01:04  来源:igfitidea点击:

How can I get a list of removable drives plugged in the computer?

pythonwindowspywin32

提问by ADAM COHEN HILLEL

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.

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

回答by CristiFati

The algorithm is straightforward:

算法很简单:

This is how it looks in Python(using PyWin32wrappers). Add any of win32con.DRIVE_*constants to drive_typestuple to get different drive types combinations:

这就是它在Python 中的样子(使用PyWin32包装器)。将任何win32con.DRIVE_*常量添加到drive_types元组以获得不同的驱动器类型组合:

code00.py:

代码00.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.
[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.

回答by S3DEV

I just had a similar question myself. Isolating @CristiFati's answer to only removable drives, I knocked together this quick (very simple) function for any new visitors to this question:

我自己也有类似的问题。将@CristiFati 对可移动驱动器的回答隔离开来,我为这个问题的任何新访问者组合了这个快速(非常简单)的功能:

Basically, just call the function and it will return a listof all removable drivesto the caller.

基本上,只需调用该函数,它就会将list所有可移动驱动器中的一个返回给调用者。

import win32api
import win32con
import win32file

def get_removable_drives():
    drives = [i for i in win32api.GetLogicalDriveStrings().split('\x00') if i]
    rdrives = [d for d in drives if win32file.GetDriveType(d) == win32con.DRIVE_REMOVABLE]
    return rdrives

For example:A call to get_removable_drives()will output:

例如:调用get_removable_drives()将输出:

['E:\']