简体   繁体   中英

A python script that prints both USB letter and USB name of a usb device connected on a computer

I am running the python script to print out the usb device letter and name of a USB connected on a computer. Below is the code.

*

import win32api
import win32file
drive_list = win32api.GetLogicalDriveStrings()
drive_list = drive_list.split("\x00")[0:-1]  
for letter in drive_list:
    if win32file.GetDriveType(letter) == win32file.DRIVE_REMOVABLE:
       print("list of drives connected: {0}".format(letter))

Here is the output of the relative script, when a USB device is connected.

*list of drives connected: D:*

GetLogicalDriveStrings cannot get the name of the drive, only the string of the drive.

You can use GetVolumeInformationW to get the name of the drive. Here is a sample:

import win32api
import win32file
drive_list = win32api.GetLogicalDriveStrings()
drive_list = drive_list.split("\x00")[0:-1]  

for letter in drive_list:
    if win32file.GetDriveType(letter) == win32file.DRIVE_REMOVABLE:
       print("list of drives connected: {0}".format(letter),end = ' ')
       volname, volsernum, maxfilenamlen, sysflags, filesystemtype = win32api.GetVolumeInformation(letter)
       print(volname)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM