Calling another terminal with python

3

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

I have tried with subprocess but it gave error:

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 bash .

How can I do this?

    
asked by anonymous 21.01.2015 / 15:53

1 answer

2

You can use the functions provided by the subprocess module to run commands on 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 gnome-terminal passing the necessary arguments so that you can execute a command, in this case, ls -l on the called terminal.

To call xterm simply change the contents of the variable, thus leaving:

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

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

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

    
21.01.2015 / 15:58