在新的终端窗口中从python执行终端命令? [英] Execute terminal command from python in new terminal window?

查看:378
本文介绍了在新的终端窗口中从python执行终端命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里的目标是在新的shell中运行新的python文件,并在现有的shell中运行现有的python文件.说我有两个文件,aaa.py和bbb.py.为了简单起见,aaa.py所做的只是...

The goal here is to run a new python file in a new shell from and existing python file in an existing shell. Say i have two files, aaa.py and bbb.py. Lets say for simplicity that all aaa.py does is...

subprocess.call('python bbb.py', shell=True)

...并且可以说bbb.py确实是...

...and lets say that bbb.py does is...

print 'It worked'

现在的目标是在终端1中运行aaa.py,并使其在终端2中启动bbb.py.

Now the goal is to run aaa.py in terminal 1 and get it to launch bbb.py in terminal 2. I would expect something like the command below to exist, but can't figure it out.

subprocess.call_in_new_window('python bb.py', shell=True)

推荐答案

通常无法从Shell进行此操作.您需要做的是运行终端程序本身,或者运行一些适合您的启动程序.对于每个终端程序,执行此操作的方法都不同.

There's no way to do this in general from a shell. What you have to do is run the terminal program itself, or some launcher program that does so for you. And the way to do that is different for each terminal program.

在某些情况下, os.startfile 会做您想要的事情,但这不会是普遍的.

In some cases, os.startfile will do what you want, but this isn't going to be universal.

另外,请注意,一般来说,您实际上需要脚本的绝对路径,因为新的终端窗口将运行新的Shell,因此不一定具有相同的工作目录.但对于示例,我将忽略它.

Also, note in general, you're going to actually need an absolute path to your script, because the new terminal window will be running a new shell and therefore won't necessarily have your same working directory. But I'll ignore that for the examples.

使用Windows cmd,最简单的方法是

With Windows cmd, the easiest way to do it is the start shell command. If the thing you start is any command-line program, including python, it will get a new cmd window. So, something like:

subprocess.call('start /wait python bb.py', shell=True)


OS X具有类似的命令, open .而且它是一个真实的程序,而不是shell命令,因此您不需要shell=True.但是,使用open运行命令行程序或脚本通常不会打开新的终端窗口.实际上,它的全部目的是允许您像在Finder中双击一样运行程序,除非它是.command文件,否则它永远不会在终端中运行任何东西.


OS X has a similar command, open. And it's a real program rather than a shell command, so you don't need shell=True. However, running a command-line program or script with open doesn't generally open a new terminal window. In fact, the whole point of it is to allow you to run programs as if they were being double-clicked in Finder, which never runs something in the terminal unless it's a .command file.

因此,您可以创建一个临时的.command包装文件,然后创建open;像这样(未经测试):

So, you can create a temporary .command wrapper file and open that; something like this (untested):

with tempfile.NamedTemporaryFile(suffix='.command') as f:
    f.write('#!/bin/sh\npython bb.py\n')
    subprocess.call(['open', '-W', f.name])

或者,您可以明确地告诉open使用Terminal.app,如下所示:

Alternatively, you can explicitly tell open to use Terminal.app, something like this:

subprocess.call(['open', '-W', '-a', 'Terminal.app', 'python', '--args', 'bb.py'])

或者您可以通过AppleEvents编写Terminal.app脚本.例如:

Or you can script Terminal.app via AppleEvents. For example:

appscript.app('Terminal').do_script('python bb.py')

执行脚本"事件将打开一个新窗口,并将其参数作为命令运行.如果要进行更详细的控制,请在AppleScript编辑器中打开脚本字典,并查看所有您可以做的有趣的事情.

The "do script" event opens a new window and runs its argument as a command. If you want more detailed control, open the scripting dictionary in AppleScript Editor and see all the fun stuff you can do.

在Linux或其他* nix系统上……那么,共有65102种不同的桌面环境,启动器和终端程序.您需要对所有这些进行处理吗?

On Linux or other *nix systems… well, there are 65,102 different desktop environments, launchers, and terminal programs. Do you need to work on all of them?

使用gnome-terminal,再次运行该终端会为您提供一个新窗口,而-x参数使您可以指定初始命令,因此:

With gnome-terminal, just running the terminal again gives you a new window, and the -x argument lets you specify an initial command, so:

subprocess.call(['gnome-terminal', '-x', 'python bb.py'])

许多较旧的终端都尝试与xterm兼容,而xterm-e的作用相同,所以:

Many older terminals try to be compatible with xterm, which does the same thing with -e, so:

subprocess.call(['xterm', '-e', 'python bb.py'])
subprocess.call(['rxvt', '-e', 'python bb.py'])

...等等

您如何知道用户正在使用哪个终端?好问题.您可以从父辈那里走走像父进程一样,直到找到看起来像终端的东西.或者您可以假设每个人都有xterm.或者,您可以查看各种发行版如何配置默认终端并搜索所有终端.或者...

How do you know which terminal the user is using? Good question. You could walk the like of parent processes from yourself until you find something that looks like a terminal. Or you could just assume everyone has xterm. Or you could look at how various distros configure a default terminal and search for all of them. Or…

这篇关于在新的终端窗口中从python执行终端命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆