Search code examples
pythonubuntusubprocess

Using subprocess.Popen messing Ubuntu terminal output


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.


Solution

  • 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')