[Python] Why am I getting an additional " at the very end of the sbrun string?

Discussão em 'Python' iniciado por Stack, Julho 24, 2025.

  1. Stack

    Stack Membro Participativo

    I am building a remote terminal with Python subprocess. Trying to capture the output and errors from subprocess.run().

    Consider a variable cmd that contains "echo helloworld". If I print it right before the code below, it doesn't change, i.e. it contains the same "echo helloworld".

    result = subprocess.run(f"cmd /c {cmd}", shell=True, capture_output=True, text=True)


    The problem is, what I receive in the output is helloworld"; there is a double quote at the end of the string that can't be printed.

    Full function code:

    def runcmd(cmd:str) -> None:
    try:
    # cmd = cmd[:-1] didnt worked. i tried to remove the last " . even .replace() didnt worked
    try:
    # added a \" because it works with most commands. kind of a temp fix
    result = sbrun(f"cmd /c \"{cmd}", shell=True, capture_output=True, text=True)
    stdout = result.stdout
    stderr = result.stderr
    exit_code = result.returncode
    except Exception as e:
    stdout = ""
    stderr = str(e)
    exit_code = -1
    if not stderr:stderr="none"
    if not stdout:stdout="none"
    output = f"OUTPUT:\t{stdout}\nERROR:\t{stderr}\nCODE:\t{exit_code}"
    alsr = post(url+"terminal",json={"output":eek:utput})
    if alsr.status_code == 200:
    log(f"{cmd} executed") # my remote logging function incase it goes bad
    output = f"OUTPUT:\t{stdout}\nERROR:\t{stderr}\nCODE:\t{exit_code}"
    alsr = post(url+"terminal",json={"output":eek:utput})# the func to send the output to me
    if not alsr.status_code == 200:
    log(f"failed to send the output. The output was\t\t OUTPUT:\t{stdout}\nERROR:\t{stderr}\nCODE:\t{exit_code}")

    except Exception as e:
    log(f"runcmd thread error:\t{e}",state="WARN")


    Why is that ghost quote there that can't be seen by any means?

    Continue reading...

Compartilhe esta Página