python - 将 Os.system 结果存储在变量中

标签 python linux variables os.system

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

相关文章:

python - Django 搜索多个过滤器

python - 数据库设计中表的对应对象

python - npm 错误!吉普 错误!堆栈错误: Could not find any Visual Studio installation to use

python - 使用 lambda getter 和 setter 创建属性

linux - 它说 "ftp localhost command not found"。当我使用命令 ftp localhost 在 Linux 中以匿名用户身份登录时

c++ - 在 64 位 Linux 上 boost 线程内存使用率

linux - 循环遍历目录中的文件并使用 HEAD 命令

c - C 中枚举类型变量的用途

Excel VBA - 声明变量的不同方法

c++ - 如何以这种方式使用变量 C++