python – 在Sublime Text的路径中执行外部程序
发布时间:2020-12-20 13:38:52 所属栏目:Python 来源:网络整理
导读:我正在尝试为Sublime编写一个插件,它将读取当前行的文本,将其作为 shell命令执行,并将命令的输出放入编辑器中.这是我到目前为止: import sublime,sublime_plugin,os,os.pathimport subprocessdef GetLineAtCursor(view): pos = view.sel()[0].a reg = view.
我正在尝试为Sublime编写一个插件,它将读取当前行的文本,将其作为
shell命令执行,并将命令的输出放入编辑器中.这是我到目前为止:
import sublime,sublime_plugin,os,os.path import subprocess def GetLineAtCursor(view): pos = view.sel()[0].a reg = view.line(pos) return view.substr(reg) class ExecuteLineGetOutputCommand(sublime_plugin.TextCommand): def run(self,edit): line = GetLineAtCursor(self.view).strip().split() output = subprocess.check_output(line,shell=True) self.view.insert(edit,output) 这不起作用.具体来说,对subprocess.check_output(…)的调用不起作用,尽管它在python解释器中工作正常.我把它放在try块中,如下所示: try: output = subprocess.check_output(line,shell=True) except Exception as e: self.view.insert(edit,str(e)) 它产生以下输出,似乎无论我尝试什么命令: [WinError 6] The handle is invalid 有谁知道问题是什么,以及如何解决它? 解决方法
试试这个:
def run(self,edit): line = GetLineAtCursor(self.view).strip().split() cmd = [line,'attr1','attr2'] # if windows p = subprocess.Popen(cmd,stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE,startupinfo=startupinfo,shell=False,creationflags=subprocess.SW_HIDE) #else unix/macos p = subprocess.Popen(cmd,stderr=subprocess.PIPE) output,stderr = p.communicate() if (stderr): print(stderr) self.view.insert(edit,output) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |