I have a script that works fine when in a .pyw, but when converted to an .exe, (EDIT: actually, when I use pyinstaller with the arguments -w
or --windowed
or --noconsole
it doesn't work, but without them it works) I've found that this one single line seems to crash the program:
firstplan = subprocess.check_output(["powercfg", "-list"], shell=True ).split('\n')[3]
Does anybody have any idea why? If I comment it out the program doesn't crash. I have two other very similar lines.
EDIT:
Maybe it'd be a good idea to put the script here...
from __future__ import print_function
import os
# os.system('cls')
import psutil
import subprocess
loop = 1
while loop == (1):
CPUload = (psutil.cpu_percent(interval=4)) # CPU load
RAMload = (psutil.virtual_memory().percent) # RAM load
# os.system('cls')
print("CPU Load: ", end="") # Display CPU Load:
print(CPUload, "%") # Display CPUload
print("RAM Load: ", end="") # Display CPU Load:
print(str(RAMload) + " %") # Display RAMload
firstplan = subprocess.check_output(["powercfg", "-list"], shell=True ).split('\n')[3] # Selects a line
secondplan = subprocess.check_output(["powercfg", "-list"], shell=True ).split('\n')[4]
thirdplan = subprocess.check_output(["powercfg", "-list"], shell=True ).split('\n')[5]
firstplanID = ((firstplan.split(": "))[1].split(" (")[0]) # Extract XplanID from Xplan
secondplanID = ((secondplan.split(": "))[1].split(" (")[0])
thirdplanID = ((thirdplan.split(": "))[1].split(" (")[0])
activeplan = subprocess.check_output(["powercfg", "/getactivescheme"]) # Find the currently active plan
activeplanNAME = ((activeplan.split("("))[1].split(")")[0]) # Extract activeplanNAME from activeplan
firstplanNAME = ((firstplan.split("("))[1].split(")")[0]) # Extract XplanNAME from Xplan
secondplanNAME = ((secondplan.split("("))[1].split(")")[0])
thirdplanNAME = ((thirdplan.split("("))[1].split(")")[0])
if "High performance" in firstplanNAME: # Identify which plan is High performance
HighPerformance = firstplanNAME
HighPerformanceID = firstplanID
if "High performance" in secondplanNAME:
HighPerformance = secondplanNAME
HighPerformanceID = secondplanID
if "High performance" in thirdplanNAME:
HighPerformance = thirdplanNAME
HighPerformanceID = thirdplanID
if "Power saver" in firstplanNAME: # Identify which plan is Power saver
PowerSaver = firstplanNAME
PowerSaverID = firstplanID
if "Power saver" in secondplanNAME:
PowerSaver = secondplanNAME
PowerSaverID = secondplanID
if "Power saver" in thirdplanNAME:
PowerSaver = thirdplanNAME
PowerSaverID = thirdplanID
if activeplanNAME == PowerSaver: # Checks current plan name
print("Active plan: Power saver")
else:
if activeplanNAME == HighPerformance:
print("Active plan: High Performance")
else:
subprocess.check_output(["powercfg", "/s", HighPerformanceID])
if CPUload < 44:
if RAMload > 90:
if activeplanNAME == PowerSaver:
subprocess.check_output(["powercfg", "/s", HighPerformanceID])
print("Switching to High Performance by RAM load...")
if CPUload < 44:
if RAMload < 90:
if activeplanNAME == HighPerformance:
subprocess.check_output(["powercfg", "/s", PowerSaverID])
print("Switching to Power saver...")
if CPUload > 55:
if activeplanNAME == PowerSaver:
subprocess.check_output(["powercfg", "/s", HighPerformanceID])
print("Switching to High Performance...")
The troubled lines are lines 21-23.
For further info, scroll down to comments and answers.