将操作系统结果存储在变量中 [英] store Os.system result in variable

查看:28
本文介绍了将操作系统结果存储在变量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我想知道如何将 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

这篇关于将操作系统结果存储在变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆