简体   繁体   English

使用子进程获取返回值

[英]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 ). 我已经检查了SCRIPT的返回值,但是,当我尝试设置此变量时,它总是返回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()

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.check_call()从脚本中获取最终返回值,0通常表示“脚本已成功完成”。

subprocess.call already returns the process return value. subprocess.call已经返回进程返回值。 If you're always getting 0 , then you'll need to review SCRIPT to make sure that it's returning the value you expect. 如果你总是得到0 ,那么你需要检查SCRIPT以确保它返回你期望的值。

To double check, you can also execute the SCRIPT in a shell and use $? 要仔细检查,你还可以在shell中执行SCRIPT并使用$? to get the return code. 获取返回码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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