Calling another terminal with python

Asked

Viewed 1,222 times

3

I’m doing a project here and I need to open another terminal window, which runs the command ls.

I’ve tried with subprocess but it was a mistake:

Traceback (most recent call last):
  File "tests.py", line 8, in <module>
    subprocess.Popen(cmd, stdout=subprocess.PIPE)
  File "/usr/lib/python2.7/subprocess.py", line 679, in __init__
    errread, errwrite)
  File "/usr/lib/python2.7/subprocess.py", line 1249, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory

My terminal is the bash.

How can I do that ?

1 answer

2


You can use the functions provided by the module subprocess to execute commands in the terminal.

import subprocess

cmd = ['gnome-terminal'] # Se estiver usando o GNOME
cmd.extend(['-x', 'bash', '-c', 'ls -l; exec $SHELL' ])

subprocess.Popen(cmd, stdout=subprocess.PIPE)

The above command will execute the gnome-terminal passing the necessary arguments so that a command can be executed, in this case, ls -l in the terminal called.

To call the xterm just change the contents of the variable, leaving so:

cmd = ['xterm']
cmd.extend(['-e', 'bash', '-c', 'ls -l; exec $SHELL' ])

subprocess.Popen(cmd, stdout=subprocess.PIPE)

The above commands apply to Bash for other interpreters the syntax can change.

  • @Qmechanic73 obitive result with this command: os.system("xterm -e 'sudo apt-get update'")

  • But with the command up the program waits for execution in xterm ends for continuing execution of the program, would have a way to proceed with the program without the execution of the?

  • @Pedrosouza Use the subprocess.Popen he does that. :)

  • Now it worked out, thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.