Python 获取U盘的序列号


是根据盘符获取的U盘序列号:

import psutil
import win32api
from io import StringIO

def run():
    uf = StringIO()
    while True:
        disk_list = []
        for item in psutil.disk_partitions():
            # 判断是不是U盘
            if "removable" in item.opts:
                # 获取U盘的盘符
                disk_list.append(item.mountpoint)
        # 把盘符写入内存,为了不持续请求
        if disk_list != []:
            for pf in disk_list:
                if pf not in uf.getvalue():
                    print("U盘插入")
                    uf.write(disk_list[0])
                    """考虑插多个u盘"""
                    seriaNumber = win32api.GetVolumeInformation(pf)
                    print(f"U盘序列号:{seriaNumber[1]}")

        else:
            # 拔出u盘初始化内存
            uf = StringIO('hello')
            print("U盘拔出")


if __name__ == "__main__":
    run()

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM