Finding the First Connected USB Device

  • Share this:

Code introduction


This function is used to find the first USB device connected to the computer.


Technology Stack : PyUSB

Code Type : Function

Code Difficulty : Beginner


                
                    
import usb.core
import usb.util

def find_first_usb_device():
    # Find all devices connected
    devices = usb.core.find(find_all=True)
    
    # If no devices found, return None
    if not devices:
        return None
    
    # Return the first device found
    return devices[0]                
              
Tags: