Search code examples
pythonpowercfg

How to skip lines of command into variable in Python


I want to use the command:

subprocess.check_output(["powercfg", "-list"])

which gives me:

Existing Power Schemes (* Active)

-----------------------------------

Power Scheme GUID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx  (Balanced)

Power Scheme GUID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx  (High performance)

Power Scheme GUID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx  (Power saver) *

which is good. But, I only want the variables in the middle to go into their own variables without all the extra text. The problem is that the command comes out in multiple lines so I can't use (list[19:55]). I also need to do this with each of the strings inside the parentheses into their own variables but I think I can figure that out. I'm new to Python so I'm open to whatever.


Solution

  • Found the answer, even though my title is poorly worded I hope someone finds this because it was hell trying to figure it out. All you have to do is change the number at the end to go down lines:

    firstplan = subprocess.check_output(["powercfg", "-list"], shell=True ).split('\n')[3]

    secondplan = subprocess.check_output(["powercfg", "-list"], shell=True ).split('\n')[4]

    etc.

    I then went on to "parse" the ID strings out (if that's the right word) with this:

    firstplanID = ((firstplan.split(": "))[1].split(" (")[0])

    and then manually set a variable for each plan.