Detect inserted USB on Windows

前端 未结 2 624
渐次进展
渐次进展 2020-12-04 15:56

I am currently writing a security tool in python that runs as a daemon on a host computer. Whenever a usb storage device is detected, it will copy all of the files from the

2条回答
  •  广开言路
    2020-12-04 16:49

    Ok there is a much simpler way to find usb devices on windows machine use following code:

    import win32file
    
    def locate_usb():
        drive_list = []
        drivebits = win32file.GetLogicalDrives()
        for d in range(1, 26):
            mask = 1 << d
            if drivebits & mask:
                # here if the drive is at least there
                drname = '%c:\\' % chr(ord('A') + d)
                t = win32file.GetDriveType(drname)
                if t == win32file.DRIVE_REMOVABLE:
                    drive_list.append(drname)
        return drive_list
    

    code was actually taken from https://mail.python.org/pipermail/python-win32/2006-December/005406.html

提交回复
热议问题