python windows 外部usb设备检测

import win32com
# 测试使用KINGSTON 金士顿u盘
list_name = ["KINGSTON"]
#windows 外部设备检测
def detect():
    """
    检测windows 的外部设备
    通过全局变量把控,用的是匹配不是相等
    return 获取到文件列表
    """
    wmi = win32com.client.Dispatch("WbemScripting.SWbemLocator")
    service = wmi.ConnectServer(".", "root\\cimv2")
    # 查询获取全部USB设备
    usb_devices = service.ExecQuery("SELECT * FROM Win32_USBControllerDevice")
    devices = []
    for usb_device in usb_devices:
        # 获取相关设备的路径
        device_id = usb_device.dependent.split("DeviceID=")[1].strip('"')
        # 可以根据usb的厂商ID和产品ID进行精确匹配
        for name in list_name:
            if name in device_id:
                devices.append(device_id)
    return devices
import win32com
# 测试使用KINGSTON 金士顿u盘
list_name = ["KINGSTON"]
#windows 外部设备检测
def detect():
    """
    检测windows 的外部设备
    通过全局变量把控,用的是匹配不是相等
    return 获取到文件列表
    """
    wmi = win32com.client.Dispatch("WbemScripting.SWbemLocator")
    service = wmi.ConnectServer(".", "root\\cimv2")
    # 查询获取全部USB设备
    usb_devices = service.ExecQuery("SELECT * FROM Win32_USBControllerDevice")
    devices = []
    for usb_device in usb_devices:
        # 获取相关设备的路径
        device_id = usb_device.dependent.split("DeviceID=")[1].strip('"')
        # 可以根据usb的厂商ID和产品ID进行精确匹配
        for name in list_name:
            if name in device_id:
                devices.append(device_id)
    return devices

如果一直开着检测,检测到就可以做一些操作,例如:

检测手机,然后将手机上的截图文件迁移或者copy

import subprocess
import os
from tqdm import tqdm
adb_path = "K:/adb/adb/adb.exe"#adb地址
local_path = "K:/photo"#放入电脑的本机地址
def folder_adb(folder_path):
    # 构建 ADB 命令
    adb_command = [adb_path, "shell", "ls", folder_path]
    # 执行 ADB 命令并获取输出
    result = subprocess.run(adb_command, capture_output=True, text=True)
    output = result.stdout
    files = output.strip().split('\n')
    num = 0
    for i in files:
        num+=1
    pbar = tqdm(range(0,num))
    for i in files:
        pbar.update()#进度条更新
        pull_command = f"{adb_path} pull {folder_path}/{i} {local_path}"
        os.system(pull_command)
        delect_command = f"{adb_path} shell rm {folder_path}/{i}"#删除语句,如果不需要可以注释
        os.system(delect_command)
folder_adb("/storage/emulated/0/Pictures/Screenshots")#手机截图地址

import subprocess
import os
from tqdm import tqdm
adb_path = "K:/adb/adb/adb.exe"#adb地址
local_path = "K:/photo"#放入电脑的本机地址
def folder_adb(folder_path):
    # 构建 ADB 命令
    adb_command = [adb_path, "shell", "ls", folder_path]
    # 执行 ADB 命令并获取输出
    result = subprocess.run(adb_command, capture_output=True, text=True)
    output = result.stdout
    files = output.strip().split('\n')
    num = 0
    for i in files:
        num+=1
    pbar = tqdm(range(0,num))
    for i in files:
        pbar.update()#进度条更新
        pull_command = f"{adb_path} pull {folder_path}/{i} {local_path}"
        os.system(pull_command)
        delect_command = f"{adb_path} shell rm {folder_path}/{i}"#删除语句,如果不需要可以注释
        os.system(delect_command)
folder_adb("/storage/emulated/0/Pictures/Screenshots")#手机截图地址

你可能感兴趣的:(python,windows,后端,驱动开发,交互)