【问题标题】:How to open Mac terminal at a specific directory using python script如何使用 python 脚本在特定目录中打开 Mac 终端
【发布时间】:2022-08-03 17:17:22
【问题描述】:
    from applescript import tell 
 
    command = \'cd Downloads/\"SSR Drive\"\'
    yourCommand = \"PERF -i \"+str(x)+\" -u CFG.txt\"
    tell.app( \'Terminal\', \'do script \"\' + command + \'\"\')
    tell.app( \'Terminal\', \'do script \"\' + yourCommand + \'\"\')

我正在使用此代码通过我的 python 脚本在 Mac 终端上执行命令。

\"command\" 告诉终端打开目录

\"yourCommand\" 告诉终端执行一个 biopython 模块函数。

但是当我执行时,命令是在单独的终端窗口上执行的。导致 \"command\" 进入目录,但随后 \"yourCommand\" 也在新终端中执行。所以我用 \"command\" 指定的目录更改不会生效。因此,\"yourCommand\" 找不到该文件。

我读到您可以更改终端的默认值以打开当前目录而不是默认目录,但这似乎是一个粗略的解决方案。

我想做的是在指定目录上打开终端,然后在终端中运行 \"yourCommand\" 代码。

TIA

    标签: python macos terminal


    【解决方案1】:

    只需将命令与&& 连接起来即可。

    from applescript import tell 
     
    command = 'cd Downloads/"SSR Drive"'
    yourCommand = "PERF -i "+str(x)+" -u CFG.txt"
    tell.app( 'Terminal', 'do script "' + command + ' && ' + yourCommand + '"')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-01
      • 2019-11-26
      相关资源
      最近更新 更多