上下文:
我正在修改一个小的 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/