1

So I am trying to execute a file and get the returned value back using the python builtin methods available in the subprocess library.

For example, lets say I want to execute this hello_world python file:

def main(): print("in main") return("hello world!") if __name__ == '__main__': main() 

I do not care about getting back the statement in main. What I want to get back is the return value hello world!.

I tried numerous things but non of them worked.

Here's a list of what I tried and their outputs:

args is common for all trials: args = ['python',hello_cmd]

First trial:

p1 = subprocess.Popen(args, stdout=subprocess.PIPE) print(p1.communicate()) print("returncode is:") print(p1.returncode) 

output is:

(b'in main\n', None) returncode is: 0 

second trial:

p2 = subprocess.check_output(args,stderr=subprocess.STDOUT) print(p2) 

output is:

b'in main\n' 

third trial:

output, result = subprocess.Popen(args, stdout = subprocess.PIPE, stderr = subprocess.PIPE, shell=False).communicate() print(output) print(result) 

output is:

b'in main\n' b'' 

fourth trial:

p4 = subprocess.run(args, stdout=PIPE, stderr=PIPE) print(p4) 

output is:

CompletedProcess(args=['python', '/path/to/file/hello.py'], returncode=0, stdout=b'in main\n', stderr=b'') 

fifth trial:

p5 =subprocess.getstatusoutput(args) print(p5) 

output is:

(0, '') 

Can anyone help?

2
  • 1
    I don't know of any operating systems that support non-numeric return codes. If you want to pass the string 'hello world!' to the parent process, you need to either go through a file or through stdout/stderr. Commented Mar 13, 2019 at 14:15
  • @0x5453 even if the return was a value instead of hello world!, still, non of the above would work! Commented Mar 13, 2019 at 14:34

3 Answers 3

3

The return value of the main function is not the return code that is passed to the OS. To pass a return code to the OS use sys.exit(), which expects an integer. You can pass it a string, but if you do, Python will pass 1 to the OS.

Sign up to request clarification or add additional context in comments.

Comments

0

You cannot return strings as return codes it must be an integer. If you want to act differently depending on the process. Try to map your return code to some function in your main program. For example

def execute_sub_program(): ... # somewhere else: return_code = execute_sub_program() if return_code == 0: # do something ... elif ... 

Comments

0

You can try with subprocess.run().returncode, it gives 0 if successful execution and 1 if failed execution.


driver.py

import subprocess args = ['python', './hello_cmd.py'] status_code = subprocess.run(args).returncode print(["Successful execution", "Failed execution"][status_code]) 

For happy flow (hello_cmd.py):

def main(): print("in main") return("hello world!") if __name__ == '__main__': main() 

For failed flow (hello_cmd.py):

def main(): print("in main") raise ValueError('Failed') if __name__ == '__main__': main() 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.