'How to get back "error" message from os.popen

So I wrote a code that runs cmd commands from discord.

async def execute_command(message):
    global pc_mac
    if "execlinuxterminal" in message.content.lower():
        command = message.content.split("execlinuxterminal ", 1)[1]
        if command.lower() == "turn on pc":
            stream = os.popen(f'''wakeonlan {pc_mac}''')
            output = stream.readlines()
            await message.channel.send(output)
        else:
            try:
                stream = os.popen(command)
                output = stream.readlines()
                rep = []
                for x in output:
                    rep.append(x.replace("\n", ""))
                rep = list(rep)
                await message.channel.send("\n".join(rep))
            except Exception as e:
                await message.channel.send(e)

It works perfectly for the most part but when I enter a command that doesn't exist it only prints it in the python program but doesn't save it in the output variable. discord.py isn't at fault the

output = stream.readlines()

doesn't return anything in case of a nonexisting command. Is there a way to fix it?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source