python - 执行 python 脚本后额外的零

标签 python python-2.7

我正在尝试从另一个脚本执行 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/

相关文章:

python - 在 django 中按数字字符串排序查询结果(postgres 后端)

python - 如何在python中检查raw_input是否为整数、字符串和日期

python - 如何通过原始输入从类中提取信息

python - 如何使用 pip 安装请求模块?是否正确安装了 pip?

python - aiohttp:如何将sub_app添加到sub_app?

python - 填充绘制线下的区域

python - Unicodedata.normalize() 值错误 : invalid normalization form

python - 我知道一些 CRC32 哈希的长度和限制字符集。这是否更容易扭转它们?

python - 将递归生成器对象转换为列表

python - 值错误 : Attempted relative import in non-package