手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

Python执行shell命令四法

[日期:2014-07-18] 来源:Linux社区  作者:koumm [字体: ]

整理:Python执行shell命令四法,示例如下:

#!/usr/bin/env python 

# -*- coding: utf-8 -*-

import os

import platform

import subprocess

import commands

def subproc():

    print "系统进程数:"

    subprocess.call("ps -ef|wc -l",shell=True)

def os_popen():

    print "IP地址:"

    os1 = platform.system()

    if os1 == "Linux":

          print os1

          ip1 =os.popen("/sbin/ifconfig eth0|grep 'inet addr'").read().strip().split(":")[1].split()[0]

          print "\033[1;32;40m%s\033[0m" % ip1

def os_system():

    os_command = 'free -m'

    cls_node1 = "命令执行成功...."

    cls_node2 = "命令执行失败...."

    if os.system(os_command) == 0:

        print "\n\033[1;32;40m%s\033[0m" % cls_node1

    else:

        print "\n\033[1;31;40m%s\033[0m" % cls_node2

def os_commands():

    (status, output) = commands.getstatusoutput('pwd')

    print status, output

def main():

    subproc()

    os_popen()

    os_system()

    os_commands()

if __name__ == "__main__":

    main()

Python调用shell命令  http://www.linuxidc.com/Linux/2012-08/67787.htm

《Python核心编程 第二版》.(Wesley J. Chun ).[高清PDF中文版] http://www.linuxidc.com/Linux/2013-06/85425.htm

《Python开发技术详解》.( 周伟,宗杰).[高清PDF扫描版+随书视频+代码] http://www.linuxidc.com/Linux/2013-11/92693.htm

Python脚本获取Linux系统信息 http://www.linuxidc.com/Linux/2013-08/88531.htm

Ubuntu下用Python搭建桌面算法交易研究环境 http://www.linuxidc.com/Linux/2013-11/92534.htm

Python 的详细介绍请点这里
Python 的下载地址请点这里

本文永久更新链接地址http://www.linuxidc.com/Linux/2014-07/104365.htm

linux
相关资讯       Python执行shell命令  Python执行shell 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款