def getACstatue(): import ctypes from ctypes import wintypes class SYSTEM_POWER_STATUS(ctypes.Structure): _fields_ = [ ('ACLineStatus', wintypes.BYTE), ] SYSTEM_POWER_STATUS_P = ctypes.POINTER(SYSTEM_POWER_STATUS) GetSystemPowerStatus = ctypes.windll.kernel32.GetSystemPowerStatus GetSystemPowerStatus.argtypes = [SYSTEM_POWER_STATUS_P] GetSystemPowerStatus.restype = wintypes.BOOL status = SYSTEM_POWER_STATUS() if not GetSystemPowerStatus(ctypes.pointer(status)): raise ctypes.WinError() print('ACLineStatus', status.ACLineStatus) return ('ACLineStatus', status.ACLineStatus)
getACstatue()
运行后出现下面信息,说明是AC状态
import psutil
# 获取电源信息
power_info = psutil.sensors_battery()
print(power_info)
print(power_info.power_plugged)
True是AC,False是DC