将操作系统结果存储在变量中
问题描述:
大家好,我想知道如何将 os.system
结果存储在变量
hello guys i'm wondering how to store os.system
result in variable
我们知道它是 return 0
as we know it's return 0
所以我想知道我应该怎么做来存储结果
so i'm wondering what i should do to store the result
和第二个问题:如何在 Linux 中获取 ip [有人会建议 ifconfig
] 但是 ifconfig
显示了这么多结果我只是想要 IP
and second question : how to get ip in Linux [ somebody will suggest ifconfig
] but ifconfig
show so many result i just wana the 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