Sublime Forum

Can't save return from called function in array

#1

Hi! I am having some troubles trying to save the return from a defined function when calling it. Standard ways as x=function(a) doesn’t work. What the function basically does is to run in the terminal a given command and to return its output. The function is:

import sys
import subprocess

def execute(command):
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

# Poll process for new output until finished
while True:
    nextline = process.stdout.readline()
    if nextline == '' and process.poll() is not None:
        break
    sys.stdout.write(nextline)
    sys.stdout.flush()
global output
output = process.communicate()[0]
exitCode = process.returncode

if (exitCode == 0):
    return output
else:
    raise ProcessException(command, exitCode, output)

and then when I call it when once I define the command and do variable=execute(command) it works and show the output but doesn’t save it in a string. I would appreciate some help! :slightly_smiling:

0 Likes