我正在尝试从另一个脚本执行 python 脚本并将输出值存储在变量中。
当我这样做时:
import os
test = os.system("/usr/local/bin/script1.py")
print test;
然后执行脚本,结果我得到一个额外的零:
$ ./test.py
171
0
script1.py 确实生成“171”作为输出,但不确定额外的零来自哪里......有什么想法吗?
最佳答案
这就是exit status命令的。
帮助(os.system)
:
system(...)
system(command) -> exit_status
Execute the command (a string) in a subshell.
On Unix, the return value is the exit status of the process encoded in the format specified for wait(). Note that POSIX does not specify the meaning of the return value of the C system() function, so the return value of the Python function is system-dependent.
最好subprocess.check_output
如果您想将命令的输出存储在变量中。
关于python - 执行 python 脚本后额外的零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19914292/