简体   繁体   中英

Get a return value using subprocess

I want to be able to define a variable by the return value of a script. This is what I currently have:

sum_total_earnings_usd = subprocess.call([SCRIPT, "-d", date])

I have checked the return value of SCRIPT, however, when I try and set this variable, it always returns 0 ( http://docs.python.org/library/subprocess.html#subprocess.call ). How would I run this script and capture the return value to store as a variable?

If your script is returning the value, then you would want to use subprocess.check_output() :

subprocess.check_output([SCRIPT, "-d", date], shell=True).

subprocess.check_call() gets the final return value from the script, and 0 generally means "the script completed successfully".

subprocess.call already returns the process return value. If you're always getting 0 , then you'll need to review SCRIPT to make sure that it's returning the value you expect.

To double check, you can also execute the SCRIPT in a shell and use $? to get the return code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM