Executing an external program in the path from Sublime Text

Viewed 1541

I'm trying to write a plugin for Sublime which will read the text of the current line, execute that as a shell command, and place the output of the command into the editor. This is what I have so far:

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, 0, output)

This is not working. Specifically, the call to subprocess.check_output(...) is not working, although it works fine in the python interpreter. I put it in a try block, like this:

try:
    output = subprocess.check_output(line,shell=True)
except Exception as e:
    self.view.insert(edit, 0, str(e))

It produces the following output, seemingly no matter what command I try:

[WinError 6] The handle is invalid

Does anybody know what the problem is, and how to fix it?

1 Answers
Related