简体   繁体   中英

Opening terminal using python

Currently I've been creating just a small program. And one of the options is to start a counter.

Code for counter

from time import sleep
number = 0
while 1 == 1:
    sleep(1)
    print number
    number += 1

I want to be able to open this program in a new terminal window (Ubuntu) so I can keep the existing terminal open. Anyway to do this in python?

Thanks

Paul

EDIT I think I figured out one way to run it in the same terminal so it works!

os.system('python counter.py')

I simply used that, I thought if I were to do that before, when I used CTRL + C it would close down both programs but this seems to of worked. Thanks for the answers!

You could create a wrapper that launches your terminal and tells it to run your script. If you know which terminal you're using, this isn't too hard. For example, if you use multi-gnome-terminal :

#!/bin/sh
multi-gnome-terminal --use-factory --command /usr/bin/python /path/to/my/script.py

Now, every time you run that wrapper (with sh ./wrapper , or /usr/local/bin/wrapper if you install -m755 it, or by double-clicking it, or whatever), it will open a new window (launching a new terminal process only if necessary) and run your script there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM