6

Using Python , I would like to start a process in a new terminal window, because so as to show the user what is happening and since there are more than one processes involved.

I tried doing:

>>> import subprocess
>>> subprocess.Popen(['gnome-terminal'])
<subprocess.Popen object at 0xb76a49ac>

and this works as I want, a new window is opened.

But how do I pass arguments to this? Like, when the terminal starts, I want it to say, run ls. But this:

>>> subprocess.Popen(['gnome-terminal', 'ls'])
<subprocess.Popen object at 0xb76a706c>

This again works, but the ls command doesn't: a blank terminal window starts.

So my question is, how do I start the terminal window with a command specified, so that the command runs when the window opens.

PS: I am targetting only Linux.

asked Jan 8, 2011 at 11:58

3 Answers 3

6
$ gnome-terminal --help-all
 ...
 -e, --command Execute the argument to this option inside the terminal
 ...

If you want the window to stay open then you'll need to run a shell or command that keeps it open afterwards.

answered Jan 8, 2011 at 12:02
Sign up to request clarification or add additional context in comments.

Comments

5
In [5]: import subprocess
In [6]: import shlex
In [7]: subprocess.Popen(shlex.split('gnome-terminal -x bash -c "ls; read -n1"'))
Out[7]: <subprocess.Popen object at 0x9480a2c>
answered Jan 8, 2011 at 12:28

Comments

0

this is the system that I use to launch a gnome-terminal from notepad++ in WINE,

1:notepad++ command to launch

#!/usr/bin/python
#this program takes three inputs:::
#1ドル is the directory to change to (in case we have path sensitive programs)
#2ドル is the linux program to run
#3ドル+ is the command line arguments to pass the program
#
#after changing directory, it launches a gnome terminal for the new spawned linux program
#so that your windows program does not eat all the stdin and stdout (grr notepad++)
import sys
import os
import subprocess as sp
dir = sys.argv[1]
dir = sp.Popen(['winepath','-u',dir], stdin=sp.PIPE, stdout=sp.PIPE).stdout.read()[:-1]
os.chdir(os.path.normpath(os.path.realpath(dir)))
print os.getcwd()
print "running '%s'"%sys.argv[2]
cmd=['gnome-terminal','-x','run_linux_program_sub']
for arg in sys.argv[2:]:
 cmd.append(os.path.normpath(os.path.realpath(sp.Popen(['winepath','-u',arg], stdin=sp.PIPE, stdout=sp.PIPE).stdout.read()[:-1])))
print cmd
p = sp.Popen(cmd, stdin=sp.PIPE, stdout=sp.PIPE)

2: run sub script, which I use to run bash after my program quits (python in this case normally)

#!/bin/sh
#1ドル is program to run, 2ドル is argument to pass
#afterwards, run bash giving me time to read terminal, or do other things
1ドル "2ドル"
echo "-----------------------------------------------"
bash
answered Jan 8, 2011 at 13:24

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.