Search code examples
pythontkintersubprocess

Printing specific items from Python Shell


I have this code:

import subprocess
activescheme = subprocess.check_output('powercfg -getactivescheme')
print(str(activescheme, encoding='cp866'))

And this output:

GUID схемы питания: 22222222-2222-2222-2222-222222222222  (Высокая производительность)

I want to check this the output ad then print message in tkinter, but I coildn't. So I think that I can take only words from brackets and print them in tk. I don't know how to do this

I tried this but didn't get a result, the output was No

import subprocess
activescheme = subprocess.check_output('powercfg -getactivescheme')
print(str(activescheme, encoding='cp866'))
if activescheme == "GUID схемы питания: 22222222-2222-2222-2222-222222222222  (Высокая производительность)":
    print("Yes")
else:
    print("No")

Help, please


Solution

  • Note that activescheme is of type bytes, not string. So the comparison will be evaluated as False.

    You can either converting activescheme to string before comparison:

    activescheme = activescheme.decode("cp866")
    if activescheme == "Power Scheme GUID: fb5220ff-7e1a-47aa-9a42-50ffbf45c673  (HP Optimized (Modern Standby))":
        print("Yes")
    else:
        print("No")
    

    Or comparing with an array of bytes:

    if activescheme == b"Power Scheme GUID: fb5220ff-7e1a-47aa-9a42-50ffbf45c673  (HP Optimized (Modern Standby))":
        print("Yes")
    else:
        print("No")