3

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.

Martin Tournoij
  • 26,737
  • 24
  • 105
  • 146
Adam
  • 299
  • 1
  • 4
  • 19

2 Answers2

6

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 python

import sys

import win32con as wcon
from win32api import GetLogicalDriveStrings
from win32file import GetDriveType


def get_drives_list(drive_types=(wcon.DRIVE_REMOVABLE,)):
    drives_str = GetLogicalDriveStrings()
    drives = (item for item in drives_str.split("\x00") if item)
    return [item[:2] for item in drives if not drive_types or GetDriveType(item) in drive_types]


def main(*argv):
    drive_filters_examples = (
        (None, "All"),
        ((wcon.DRIVE_REMOVABLE,), "Removable"),
        ((wcon.DRIVE_FIXED, wcon.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("{:s} drives:".format(display_text))
        for drive in drives:
            print("{:s}  ".format(drive), end="")
        print("\n")


if __name__ == "__main__":
    print("Python {:s} {:03d}bit on {:s}\n".format(" ".join(elem.strip() for elem in sys.version.split("\n")),
                                                   64 if sys.maxsize > 0x100000000 else 32, sys.platform))
    rc = main(*sys.argv[1:])
    print("\nDone.")
    sys.exit(rc)

Output:

[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q041465580]> "e:\Work\Dev\VEnvs\py_pc064_03.09_test0\Scripts\python.exe" ./code00.py
Python 3.9.9 (tags/v3.9.9:ccb0e6a, Nov 15 2021, 18:08:50) [MSC v.1929 64 bit (AMD64)] 064bit on win32

All drives:
C:  D:  E:  F:  G:  H:  I:  L:  M:  N:

Removable drives:
H:  I:

Fixed and CDROM drives:
C:  D:  E:  F:  G:  L:  M:  N:


Done.

As a side note, in my environment (at this point):

  • D: is a partition on an external (USB) HDD

  • H:, I: are partitions on a bootable USB stick (UEFI)

  • The rest are partitions on the (internal) SSD and / or HDD disks

CristiFati
  • 38,250
  • 9
  • 50
  • 87
  • How can i read files inside the DRIVE_REMOVABLE's drive? Thanks – Jim Aug 04 '17 at 12:51
  • @Jim The file system does all the underlying work for you, so you read the file just as it was residing on one of the fixed drives (by specifying its path). For example _C:\some\_file\_on\_the\_fixed\_drive.txt_ is a file on a fixed drive (assuming _C:_ is your _SYSTEM_ drive), while if you insert an USB stick, _Win_ automatically _mounts_ it to let's say _F:_, you would refer to a file on the removable drive as _F:\some\_file\_on\_the\_removable\_drive.txt_. – CristiFati Aug 09 '17 at 09:13
  • Thanks for help, USB stick works fine, but when im using Android phone for test, and it popup a dialog says `THERE IS NO DISK IN THE DRIVE. PLEASE INSERT A DISK INTO DRIVE H:`, maybe i need to leand about how to use ADB Shell... Thanks Again <3 – Jim Aug 12 '17 at 13:23
  • 1
    @Jim: Sorry for the delay, I missed your comment. The phone is not seen as a removable drive, because it doesn't expose that interface. I'm not sure if you could install a 3rd party software on phone and/or PC to achieve that functionality. – CristiFati Apr 10 '18 at 11:09
  • provide information about the dependencies and how to install them. – Omid Ataollahi Jul 06 '20 at 11:55
  • This is not working for larger, USB connected, terabyte sizes external drives - which are reported as fixed. – 576i Sep 03 '21 at 10:02
  • @576i: That's probably due to a misinterpretation of *DRIVE\_REMOVABLE* / *DRIVE\_FIXED* meaning. Looking at the table from the 2nd *URL*, it turns out it's correct. – CristiFati Jun 04 '22 at 08:38
  • @OmidAtaollahi: Question is tagged with *PyWin32*, so that's the only dependency. `python -m pip install pywin32`. Check https://stackoverflow.com/questions/58631512/pywin32-and-python-3-8-0/58632354#58632354 for some interesting insights. – CristiFati Jun 04 '22 at 08:46
2

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:

Basically, just call the function and it will return a list of all removable drives to the caller.

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:

['E:\\']
S3DEV
  • 8,768
  • 3
  • 31
  • 42