简体   繁体   中英

open terminal run command python

I am trying to open a terminal and run a command in it. I am using

os.system("gnome-terminal -e 'bash -c \"exec bash; MY_COMMAND; exec bash\" '")

This opens up a new terminal, but the command is not executed.

The exec command replaces the currently running process with a new one, so if you have an exec in a list of commands to run, as soon as exec is run, nothing else will run. So you're replacing 'bash -c \\"exec bash; MY_COMMAND; exec bash\\" ' with bash , and then nothing after the exec bash is running. Try this instead:

os.system("gnome-terminal -e 'bash -c \"MY_COMMAND\" '")

or if you need a terminal to stay open, try this:

os.system("gnome-terminal -e 'bash -c \"MY_COMMAND; sleep 1000000\" '")

of if you want the terminal to stay open and be in a bash shell, try this:

os.system("gnome-terminal -e 'bash -c \"MY_COMMAND; bash\" '")

Here we go...

command="python3 --version"
os.system("gnome-terminal -e 'bash -c \""+command+";bash\"'")

That should do it...

Output:Python 3.6.4

And the output came into a new terminal....

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