从 python 脚本返回值的最佳方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18231415/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 10:15:59  来源:igfitidea点击:

Best way to return a value from a python script

pythonreturnreturn-value

提问by ofer.sheffer

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).

我在 python 中编写了一个脚本,它需要几个文件,运行一些测试并计算 total_bugs 的数量,同时编写包含每个文件信息的新文件(错误+更多)。

To take a couple files from current working directory:

从当前工作目录中获取几个文件:

myscript.py -i input_name1 input_name2

myscript.py -i input_name1 input_name2

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.

完成这项工作后,我希望脚本“返回 total_bugs”,但我不确定实现这一点的最佳方式。

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.

一些帮助(注释/提示/代码示例)在这里可能会有所帮助。

Btw, this needs to work for windows and unix.

顺便说一句,这需要适用于 windows 和 unix。

采纳答案by Torxed

If you want your script to returnvalues, 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:

如果您希望您的脚本返回值,只需return [1,2,3]从包装您的代码的函数中执行,但您必须从另一个脚本中导入您的脚本才能使用该信息:

Return values (from a wrapping-function)

返回值(来自包装函数)

(again, this would have to be run by a separate Python script and be imported in order to even do any good):

(同样,这必须由一个单独的 Python 脚本运行并被导入,以便做任何好事):

import ...
def main():
    # calculate stuff
    return [1,2,3]

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)

(这通常适用于当您想向管理器指示出了什么问题或只是计数的错误/行数或 w/e 时。通常 0 是一个好的退出,>=1 是一个坏的退出,但您可以交互- 以任何你想要从中获取数据的方式来谈论它们)

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.

并根据您希望告诉您的州长的内容以特定的退出代码退出。我在通过调度和监控环境运行脚本时使用退出代码来指示发生了什么。

Stdout as your relay

标准输出作为你的中继

If not you'd have to use stdoutto 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.

如果不是,您将不得不使用stdout与外界进行通信(如您所描述的)。但这通常是一个坏主意,除非它是一个解析器执行您的脚本并且可以捕获您报告的任何内容。

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.

您是否在受控的调度环境中运行脚本,然后退出代码是最好的方法。

Files as conveyors

作为传送带的文件

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')

And pick up the value/result from there. You could even do it in a CSV format for others to read simplistically.

并从那里获取值/结果。您甚至可以将其以 CSV 格式进行操作,以便其他人可以简单地阅读。