Python& macOS:从Python打开新的终端窗口,传递要执行的命令

时间:2018-01-23 18:14:12

标签: python macos terminal

我使用以下两行Python代码从Python脚本中打开一个新的终端窗口,这很好用:

import os
os.system('open -a Terminal .')

现在我想将新的终端窗口传递给要执行的命令,例如

ls

我该怎么做?

2 个答案:

答案 0 :(得分:1)

尝试一下

import appscript

appscript.app('Terminal').do_script('ls')  # or any other command you choose

答案 1 :(得分:1)

由于旧答案已贬值,

如果没有的话,下载applescript,

pip3 install applescript

python脚本

from applescript import tell

#set what command you want to run here
yourCommand = 'ls'

tell.app( 'Terminal', 'do script "' + yourCommand + '"') 
相关问题