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

Jim Byrnes jf_byrnes at comcast.net
Wed Jul 9 16:00:16 CEST 2014


On 07/08/2014 06:39 PM, Alan Gauld wrote:
> 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.

Alan and the others that offered advice, thanks. I see that I have taken 
the wrong approach.  My intent was to automate something I was doing 
manually time after time and learn a little more Python as well.

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

<snip>

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

My mistake.  I went to the Docs page and clicked on modules and then os. 
For some reason as I was scrolling down the page I thought the  subject 
had changed and stopped reading.  Now I see I stopped reading to soon as 
it is much further down the page.

Thanks,  Jim



More information about the Tutor mailing list