Search code examples
pythonpowercfg

Confirm current power plan via python


I'm trying to automate checking what the current power plan is and then return a response based on that. Here's what I have:

import subprocess

plan = subprocess.call("powercfg -getactivescheme")
if 'Balanced' in plan:  
    print Success

When I run this I get "TypeError: argument of type 'int' is not iterable"

Can someone please tell me what I'm doing wrong here?


Solution

  • subprocess.call returns a code, which means the status code of the executed command.

    I also recommend you calling subprocess like this:

    subprocess.call(["powercfg", "-getactivescheme"])
    

    As I guess you want to get the output in a variable I recommend you using subprocess.check_output which returns a string containing the output of a command:

    output = subproccess.check_output(["powercfg", "-getactivescheme"])
    

    Then you can do the checking:

    if 'Balanced' in output:  
        print 'Success'
    

    Hope it helps,