skip to Main Content

Using subprocess.Popen messing Ubuntu terminal output

import subprocess
import time

subprocess.Popen("sudo ./agt_internal", shell=True, cwd="/home/boblittle/AGT/")
time.sleep(10)

for i in range(10):
    print('Good Morning America')

The terminal output is messed up. Any suggestions?
enter image description here

def start_agt_pmlog():
    command = 'sudo ./agt_internal -unilog=PM'
    args = shlex.split(command)
    print(f'Starts AGT PMLog')
    subprocess.Popen(args, shell=False, cwd="/home/boblittle/AGT/", stdout=subprocess.PIPE, stderr=subprocess.PIPE)

I chaged the code with a suggested solution by adding the function above. It didn’t help.

2

Answers


  1. Chosen as BEST ANSWER

    Found a simple solution. Adding subprocess.run("reset", shell=True). It clears the terminal but it's good enough for me.

    import subprocess
    import time
    
    subprocess.Popen("sudo ./agt_internal", shell=True, cwd="/home/boblittle/AGT/")
    time.sleep(10)
    
    subprocess.run("reset", shell=True)
    for i in range(10):
        print('Good Morning America')
    

  2. You should use shlex.split(), you have a missing double quote and also you should pass the stdout and stderr parameters to subprocess.Pope to avoid your terminal messing up.

    import subprocess
    import time
    import shlex
    
    command = "sudo ./agt_internal"
    args = shlex.split(command)
    
    subprocess.Popen(args, shell=False, cwd="/home/boblittle/AGT/", stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    time.sleep(10)
    
    for i in range(10):
        print('Good Morning America')
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search