Search code examples
pythonpython-3.xwindowshardwareram

Check which RAM slots are empty in Python


My PC has 4 slots to install RAM (memory). I have RAM installed only in the second slot, the rest are empty. This is a Windows PC.

Is it possible to determine which slots have RAM installed from Python?


Solution

  • Make sure you have win32api installed.

    import win32com.client
    
    strComputer = "."
    objWMIService = win32com.client.Dispatch("WbemScripting.SWbemLocator")
    objSWbemServices = objWMIService.ConnectServer(strComputer,"root\cimv2")
    colItems = objSWbemServices.ExecQuery("SELECT * FROM Win32_PhysicalMemory")
    for objItem in colItems:
        if objItem.BankLabel != None:
            print "BankLabel:" + ` objItem.BankLabel`
    
    

    Code borrowed and excerpted from:
    https://www.activexperts.com/admin/scripts/wmi/python/0344/

    Further reading:
    https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-physicalmemory