从 python 脚本返回值的最佳方法 [英] Best way to return a value from a python script
问题描述
我用 python 编写了一个脚本,它需要几个文件,运行一些测试并计算 total_bugs 的数量,同时为每个文件编写包含信息的新文件(错误 + 更多).
I wrote a script in python that takes a few files, runs a few tests and counts the number of total_bugs while writing new files with information for each (bugs+more).
从当前工作目录中取出几个文件:
To take a couple files from current working directory:
myscript.py -i input_name1 input_name2
myscript.py -i input_name1 input_name2
完成这项工作后,我希望脚本能够返回 total_bugs",但我不确定实现此目的的最佳方法.
When that job is done, I'd like the script to 'return total_bugs' but I'm not sure on the best way to implement this.
目前,脚本打印如下内容:
Currently, the script prints stuff like:
[working directory]
[files being opened]
[completed work for file a + num_of_bugs_for_a]
[completed work for file b + num_of_bugs_for_b]
...
[work complete]
在这里提供一些帮助(注释/提示/代码示例)可能会有所帮助.
A bit of help (notes/tips/code examples) could be helpful here.
顺便说一句,这需要适用于 windows 和 unix.
Btw, this needs to work for windows and unix.
推荐答案
如果你想让你的脚本返回值,只需执行 return [1,2,3]
来自包装您的代码的函数,但是您必须从另一个脚本导入您的脚本才能使用该信息:
If you want your script to return values, just do return [1,2,3]
from a function wrapping your code but then you'd have to import your script from another script to even have any use for that information:
(同样,这必须由单独的 Python 脚本运行并导入才能发挥作用):
(again, this would have to be run by a separate Python script and be imported in order to even do any good):
import ...
def main():
# calculate stuff
return [1,2,3]
作为指标的退出代码
(这通常只适用于当您想向管理器指示出了什么问题,或者只是计数的错误/行数或 w/e 时.通常 0 是一个好的退出,>=1 是一个错误的退出但你可以以任何你想要从中获取数据的方式对它们进行交互)
Exit codes as indicators
(This is generally just good for when you want to indicate to a governor what went wrong or simply the number of bugs/rows counted or w/e. Normally 0 is a good exit and >=1 is a bad exit but you could inter-prate them in any way you want to get data out of it)
import sys
# calculate and stuff
sys.exit(100)
并根据您想要告诉您的州长的内容以特定的退出代码退出.我在调度和监控环境运行脚本时使用退出代码来指示发生了什么.
And exit with a specific exit code depending on what you want that to tell your governor. I used exit codes when running script by a scheduling and monitoring environment to indicate what has happened.
(os._exit(100)
也能用,而且更有力一些)
(os._exit(100)
also works, and is a bit more forceful)
如果不是,您就必须使用 stdout 与外界交流(如您所描述的).但这通常是一个坏主意,除非它是一个解析器执行您的脚本并且可以捕获您报告的任何内容.
If not you'd have to use stdout to communicate with the outside world (like you've described). But that's generally a bad idea unless it's a parser executing your script and can catch whatever it is you're reporting to.
import sys
# calculate stuff
sys.stdout.write('Bugs: 5|Other: 10\n')
sys.stdout.flush()
sys.exit(0)
您是否在受控的调度环境中运行脚本,然后退出代码是最好的方法.
Are you running your script in a controlled scheduling environment then exit codes are the best way to go.
还可以选择简单地将信息写入文件,并将结果存储在那里.
There's also the option to simply write information to a file, and store the result there.
# calculate
with open('finish.txt', 'wb') as fh:
fh.write(str(5)+'\n')
然后从那里获取值/结果.您甚至可以将其转换为 CSV 格式,以便其他人能够简单地阅读.
And pick up the value/result from there. You could even do it in a CSV format for others to read simplistically.
如果以上方法都不奏效,您也可以在本地使用网络套接字 *(在 nix 系统上,unix 套接字是一种很好的方法).这些有点复杂,值得他们自己的帖子/答案.但是编辑将其添加到此处,因为它是在进程之间进行通信的一个不错的选择.特别是如果他们应该运行多个任务并返回值.
If none of the above work, you can also use network sockets locally *(unix sockets is a great way on nix systems). These are a bit more intricate and deserve their own post/answer. But editing to add it here as it's a good option to communicate between processes. Especially if they should run multiple tasks and return values.
这篇关于从 python 脚本返回值的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!