2

Is possible to get only the removable USB drives and (not so necessary) their tags on Windows using Python? On Linux (Ubuntu), you only need to list the /media folder.

Here's my current code (it lists all available drives letters including system drive, cd/dvd drives, etc):

import win32api

dv = win32api.GetLogicalDriveStrings()
dv = dv.split('\000')[:-1]
print dv

The result is something like this: ['C:\\', 'D:\\', 'E:\\']

I'd like only the USB mass storage drives... any help?

Regards...

S3DEV
  • 8,768
  • 3
  • 31
  • 42
Hairo
  • 2,062
  • 5
  • 27
  • 33
  • `/media` can contain more than just USB devices. HDD can be there, too. – Lev Levitsky Sep 04 '12 at 15:35
  • i know but that's just an example and it get the drives tags, also, it won't list the system drive :) – Hairo Sep 04 '12 at 16:13
  • Does this answer your question? [How can I get a list of removable drives plugged in the computer?](https://stackoverflow.com/questions/41465580/how-can-i-get-a-list-of-removable-drives-plugged-in-the-computer) – CristiFati Nov 03 '19 at 09:39

5 Answers5

1
import win32file

def rdrive(d):
    # returns boolean, true if drive d is removable
    return win32file.GetDriveType(d)==win32file.DRIVE_REMOVABLE
1

I just had a similar question myself. Using logic from @user2532756's answer, I knocked together this quick function for any new visitors to this question:

Very 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
1

I was looking for this too, but then made it "from scratch" using the wmi module.

import wmi

LOCAL_MACHINE_CONNECTION = wmi.WMI()

# .Win32_LogicalDisk() returns a list of wmi_object objects. Each of them represents a storage device connected to the local machine (serial number, path, description, e.t.c)
drives_available = [wmi_object.deviceID for wmi_object in LOCAL_MACHINE_CONNECTION.Win32_LogicalDisk() if wmi_object.description == "Removable Disk"]

# .deviceID is the tag (or letter like F:, G:)
Tronzen
  • 11
  • 2
0

You can also use the ctypes module to get the removable drives list.

from ctypes import windll    
def get_drives():
        drives = []
        bitmask = windll.kernel32.GetLogicalDrives()
        for letter in map(chr, range(65, 91)):
            if bitmask & 1:
                drives.append(letter)
            bitmask >>= 1
        return drives

You can take the same list and compare it with fresh list of drives to get removable drives added .

    drives_list=get_drives()
    drives_list1=[]
    while True:
      drives_list1=get_drives()
      if len(list(set(drives_list1) - set(drives_list))) > 0:
            print("Drives Added"+str(set(drives_list1) - set(drives_list)))

For more refer this repo drivemonitoring on github.

Ajay Kumar K K
  • 321
  • 2
  • 7
0

The psutil library is much better than win32api. It provides lots of information about the drives and nicely formatted. https://github.com/giampaolo/psutil

import psutils as p
externals = [i.mountpoint for i in p.disk_partitions() if 'removable' in i.opts]
anishtain4
  • 2,342
  • 2
  • 17
  • 21