[Tutor] How can I open and use gnome-terminal from a Python script?

Alan Gauld alan.gauld at btinternet.com
Wed Jul 9 01:39:53 CEST 2014


On 08/07/14 21:45, Jim Byrnes wrote:
> I would like to automate running virtualenv with a python script by:
>
> opening gnome-terminal
> cd to proper directory
> run source /bin/activate

Thats almost certainly the wrong approach.
Instead of trying to automate what the user does in the terminal replace 
the terminal with Python.

Run the cd command from within Python (os.chdir())
Run Activate from within Python (this is where os.system()
could be used, but subprocess.call() is considered better
practice.

> I found some examples of using os.system() to get gnome-terminal to open
> but I can't figure out how then cd to the proper directory in the new
> terminal.

You can't. os.system() just runs a command it has no way to interaxct5 
with the command, you do that manually. Its perfectly fine for 
displaying a directory listing (although os.listdir() would be better) 
or sending a file to a printer, or even opening a terminal/editor
for the user to interact with. But its no good for your program 
interacting with it. Thats what subprocess is for.


> My biggest frustration right now is that I can't find any documentation
> on how to use os.system().  Looking at the docs on the Python site I
> don't see system() under the os module.

You should, although they don;t have a lot to say...

----------------------
os.system(command)

Execute the command (a string) in a subshell. This is implemented by 
calling the Standard C function system(), and has the same limitations. 
Changes to sys.stdin, etc. are not reflected in the environment of the 
executed command. If command generates any output, it will be sent to 
the interpreter standard output stream.

On Unix, the return value is the exit status of the process encoded in 
the format specified for wait(). Note that POSIX does not specify the 
meaning of the return value of the C system() function, so the return 
value of the Python function is system-dependent.

On Windows, the return value is that returned by the system shell after 
running command. The shell is given by the Windows environment variable 
COMSPEC: it is usually cmd.exe, which returns the exit status of the 
command run; on systems using a non-native shell, consult your shell 
documentation.

The subprocess module provides more powerful facilities for spawning new 
processes and retrieving their results; using that module is preferable 
to using this function. See the Replacing Older Functions with the 
subprocess Module section in the subprocess documentation for some 
helpful recipes.

Availability: Unix, Windows.
-------------------------------

>  Googling hasn't helped.

When you know the module use the browser search tools on the page itself 
- that's how I found it. I searched for os.system and it
was the 3rd occurence.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list