python脚本在sublime text中打开文件

标签 python xcode sublimetext2 lldb

上下文:

我正在修改一个小的 python 脚本,以便我从 Xcode 调试器运行的 lldb 命令的输出将输出到一个文件并在 sublime text 中打开。

import lldb
import os
import subprocess

def print_to_file(debugger, command, result, dict):
  #Change the output file to a path/name of your choice
  f=open("/Users/venkat13/pcomponents.txt","w")
  debugger.SetOutputFileHandle(f,True);
  debugger.HandleCommand(command)
  path = "/Users/venkat13/pcomponents.txt"
  sublimePath = '/Applications/Sublime\ Text.app'
  subprocess.Popen("%s %s" % (sublimePath, path))


def __lldb_init_module (debugger, dict):
  debugger.HandleCommand('command script add -f po.print_to_file print_to_file ')

问题:

上面的脚本正在生成文件,但它无法在 Sublime Text 中打开。我哪里出错了?

最佳答案

这是因为 Popen 不会直接将整个字符串传递给 shell,除非您指定 shell=True (在我看来,您希望该命令的工作方式与您通常键入的方式相同)一个外壳)。

改为使用:

subprocess.Popen([sublimePath, path])

或者,(不推荐):

subprocess.Popen("%s %s" % (sublimePath, path), shell=True)

关于python脚本在sublime text中打开文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37287886/

相关文章:

python - ValueError : Error when checking target: expected dense_3 to have shape (1, ) 但得到形状为 (5,) 的数组

python - 使用 save_word2vec_format 以二进制格式 .bin 保存 gensim Word2vec 模型

Python Twisted 如何使用新的 IPolicyForHTTPS 为 twisted.web.client.Agent 设置 SSL 方法?

c++ 在同一行使用 cout 和 cin 的问题 (xcode8)

xcode - 应用程序包不包含有效标识符

swift - 键盘上方的 View 不显示

ruby - 如何停止 sublime text 构建中的无限循环

python - "or"条件导致 "if"出现问题

sublimetext2 - 将 Origami 生成的 Pane 布局保存在 Sublime Text 2 中

python - Sublime Text 语法高亮 Jinja2