Home > Back-end >  Python echo binary output
Python echo binary output

Time:08-20

I'm trying to create a GUI for spicetify-cli using PySimpleGUI. The issue I'm having is whenever I try to save the current username to a variable using user = os.system('echo %username%'), I keep receiving an output with a binary integer along with the username. The error is as follows:

tingt
0

My current code for the script isn't much at all:

import PySimpleGUI as sg
import os
import sys

user = os.system('echo %username%')
print(user)

def main():
    layout = [
        [sg.T('Welcome to Spicetify!')],
        [sg.B('Config'), sg.B('Exit')]]
    window = sg.Window('Main', layout, modal=False)
    while True:
        event, values = window.read()
        if event == 'Config':
            sg.popup_scrolled()
        if event == 'Exit':
            return
        if event == sg.WIN_CLOSED:
            return
    window.close()

main()

Is there any fix I could apply to my script?

CodePudding user response:

actually os.system will return an exit code, which is 0 that is that value of user, and will print the result to the screen, but will not save it to user.

instead you should use subprocess.getoutput

import subprocess
user = subprocess.getoutput('echo %username%')
  • Related