大家好,我想知道如何将 os.system
结果存储在变量中
我们知道它是返回 0
所以我想知道我应该做什么来存储结果
第二个问题:如何在 Linux 中获取 ip [有人会建议 ifconfig
] 但是 ifconfig
显示这么多结果我只是想要 IP
最佳答案
import os
from subprocess import *
def run_cmd(cmd):
p = Popen(cmd, shell=True, stdout=PIPE)
output = p.communicate()[0]
return output
关于第二个问题,见http://www.cyberciti.biz/tips/read-unixlinux-system-ip-address-in-a-shell-script.html
关于python - 将 Os.system 结果存储在变量中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6276614/