【发布时间】:2021-09-19 04:58:17
【问题描述】:
我想从os.system("nslookup google.com") 获取输出,但在打印时我总是得到0。为什么会这样,我该如何解决? (Python 3、Mac)
(我看了How to store the return value of os.system that it has printed to stdout in python?-但没看懂~我是python新手)
【问题讨论】:
-
Python 的
os.system("ls")仅返回ls的 exit_code,这是来自操作系统的进程的 unix 整数状态。这里的 0 表示“无错误”。 os.system 的 stdout 和 stderr 都通过管道传输到 python 程序的 stdout 和 stderr 中。因此,您要么手动重新实现此标准输出重定向,要么使用不同的 python 函数自动为您工作,其中一个例子是subprocess。
标签: python python-3.x subprocess os.system