0

If I have a script 'sp_test.py' as follows:

import sys

def sp_test(a=0,b=0,c=0):

    print(a)
    print(b)
    print(c)

    return str(a)+str(b)+str(c)

if __name__ == "__main__":
    sp_test(sys.argv[1],sys.argv[2],sys.argv[3])

If I want to call it from another python script I can use :

import sys
import subprocess

subprocess.call([sys.executable, 'sp_test.py', '1','2','3'])

However I would expect the output to be:

1
2
3
123

The output is actually:

1
2
3
0

Why is this?

I noticed this when I was trying methods to call this script from another python script - the return value seems incorrect - is this the expected behavior? If so why?

user3062260
  • 1,584
  • 4
  • 25
  • 53
  • 2
    or you can just import the function and call it normally. Anyway, `call` returns the exit code of the process, not its output. Do a quick search, there are a lot of questions like this on SO, for example https://stackoverflow.com/questions/4760215/running-shell-command-and-capturing-the-output – DeepSpace Dec 19 '19 at 12:53
  • 1
    The `0` would appear to be the exit status of `sys.executable`, though it's not clear why it's being printed. It certainly is not the return value of `sp_test`. – chepner Dec 19 '19 at 12:56
  • @chepner If OP is using the REPL when doing `subprocess.call([sys.executable, 'sp_test.py', '1','2','3'])` it would explain it, no? – DeepSpace Dec 19 '19 at 12:57
  • Thanks for the comments all, it looks like it was 'subprocess.check_output' that I needed to get the return output - I just wasn't sure the right search words to get a good answer! – user3062260 Dec 19 '19 at 13:28

0 Answers0