`
ijavagos
  • 浏览: 1190180 次
  • 性别: Icon_minigender_2
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

Python 执行 系统命令

 
阅读更多

google上搜了一下,发现在Python 里调用系统命令的方法有N种。

1. 使用os.popen

>>> import os

>>> pipe=os.popen("df -lh").read()

>>> print pipe

/dev/sda6 2.0G 333M 1.6G 18% /

/dev/sda1 99M 16M 79M 17% /boot

tmpfs 948M 0 948M 0% /dev/shm

/dev/sda7 115G 96G 14G 88% /home

/dev/sda2 7.8G 3.3G 4.2G 45% /usr

/dev/sda5 3.9G 302M 3.4G 9% /va

这种方式可以将返回结果赋给一个变量,可以在程序中处理这些结果。 这种方法比较方便。

2. 使用os.system()

>>> import os

>>> os.system('df -lh')

/dev/sda6 2.0G 333M 1.6G 18% /

/dev/sda1 99M 16M 79M 17% /boot

tmpfs 948M 0 948M 0% /dev/shm

/dev/sda7 115G 96G 14G 88% /home

/dev/sda2 7.8G 3.3G 4.2G 45% /usr

/dev/sda5 3.9G 302M 3.4G 9% /var

这个命令仅在终端执行命令。

3. 使用 subprocess.call()

>>> import subprocess

>>> subprocess.call(['df','-lh'])

/dev/sda6 2.0G 333M 1.6G 18% /

/dev/sda1 99M 16M 79M 17% /boot

tmpfs 948M 0 948M 0% /dev/shm

/dev/sda7 115G 96G 14G 88% /home

/dev/sda2 7.8G 3.3G 4.2G 45% /usr

/dev/sda5 3.9G 302M 3.4G 9% /var

0

>>>

subprocess.call(*popenargs, **kwargs)

Run command with arguments. Wait for command to complete, then return the returncode attribute.

4. 使用subprocess.Popen(command, shell=True)

帮助文档对该方法说明:

class subprocess.Popen(args, bufsize=0, executable=None, stdin=None, stdout=None, stderr=None, preexec_fn=None, close_fds=False, shell=False, cwd=None, env=None, universal_newlines=False, startupinfo=None, creationflags=0)

Arguments are:

args should be a string, or a sequence of program arguments. The program to execute is normally the first item in the args sequence or the string if a string is given, but can be explicitly set by using the executable argument. When executable is given, the first item in the args sequence is still treated by most programs as the command name, which can then be different from the actual executable name. On Unix, it becomes the display name for the executing program in utilities such as ps.

On Unix, with shell=False (default): In this case, the Popen class uses os.execvp() to execute the child program. args should normally be a sequence. If a string is specified for args, it will be used as the name or path of the program to execute; this will only work if the program is being given no arguments.

>>> import subprocess

>>> cmd='df -lh'

>>> subprocess.Popen(cmd,shell=True)

<subprocess.Popen object at 0xb7eab26c>

/dev/sda6 2.0G 333M 1.6G 18% /

/dev/sda1 99M 16M 79M 17% /boot

tmpfs 948M 0 948M 0% /dev/shm

/dev/sda7 115G 96G 14G 88% /home

/dev/sda2 7.8G 3.3G 4.2G 45% /usr

/dev/sda5 3.9G 302M 3.4G 9% /var

关于这些命令的具体说明,可以参考联机文档。

------------------------------------------------------------------------------

Blog http://blog.csdn.net/tianlesoftware

网上资源: http://tianlesoftware.download.csdn.net

相关视频:http://blog.csdn.net/tianlesoftware/archive/2009/11/27/4886500.aspx

DBA1 群:62697716(); DBA2 群:62697977()

DBA3 群:62697850 DBA 超级群:63306533;

聊天 群:40132017

--加群需要在备注说明Oracle表空间和数据文件的关系,否则拒绝申请

分享到:
评论

相关推荐

    日常整理python执行系统命令的常见方法(全)

    本文是小编日常整理的些关于python执行系统命令常见的方法,比较全面,特此通过脚本之家这个平台把此篇文章分享给大家供大家参考

    Python3 执行系统命令并获取实时回显功能

    主要介绍了Python3 执行系统命令并获取实时回显功能,文中通过两种方法给大家介绍了Python执行系统命令并获得输出的方法,需要的朋友可以参考下

    python执行系统命令后获取返回值的几种方式集合

    今天小编就为大家分享一篇python执行系统命令后获取返回值的几种方式集合,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

    Python中执行系统命令常见的方法

    Python中执行系统命令常见的几种方法.doc

    Python3 执行Linux Bash命令的方法

    和之前C++执行Linux Bash命令的方法 一样,Python依然支持system调用和popen()函数来执行linux bash命令。 方法一:system调用 #仅仅在一个子终端运行系统命令,而不能获取命令执行后的返回信息 import os os....

    Python执行Linux系统命令的4种方法

    主要介绍了Python执行Linux系统命令的4种方法,即在Python脚本中调用Shell命令,需要的朋友可以参考下

    在Python中执行系统命令的方法示例详解

    最近在做那个测试框架的时候发现对python执行系统命令不太熟悉,所以想着总结下,下面这篇文章主要给大家介绍了关于在Python中执行系统命令的方法,需要的朋友可以参考借鉴,下面来一起看看吧。

    Python如何执行系统命令

    但Python执行系统命令这个问题,从学Python之初就一直在困扰,到最近又反复几次在上边遇到问题,实在是受不了。 二、Python执行系统命令的实现方式 2.1 执行命令不需要获取命令输出–os.system 有时候我们只需要执行...

    解决python执行不输出系统命令弹框的问题

    今天小编就为大家分享一篇解决python执行不输出系统命令弹框的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

    Python如何调用外部系统命令

    前言 利用Python调用外部系统命令的方法可以提高编码效率。...某些场景调用外部命令就是为获取输出结果,也可以通过输出结果来判断命令执行成功还是失败。分析结果如下: 下面再针对每一个函数使用

    Python远程linux执行命令实现

    #远程登陆操作系统 def ssh(sys_ip,username,password,cmds): try #创建ssh客户端 client = paramiko.SSHClient() #第一次ssh远程时会提示输入yes或者no client.set_missing_host_key_policy(paramiko....

    Python-Devops定时调用http接口定时执行SSH命令的WEB定时任务工具

    Devops定时调用http接口,定时执行SSH命令的WEB定时任务工具。本系统强依赖Flask-APScheduler的功能,只是拓展了web页面部分。使用Python3进行开发。

    通过网页执行bash python等系统命令.zip

    Python使用技巧,实战应用开发小系统参考资料,源码参考。经测试可运行。 详细介绍了一些Python框架的各种功能和模块,以及如何使用Python进行GUI开发、网络编程和跨平台应用开发等。 适用于初学者和有经验的开发者...

    Python如何通过subprocess调用adb命令详解

    本文主要给大家介绍了关于使用Python通过subprocess调用adb命令,subprocess包主要功能是执行外部命令(相对Python而言)。和shell类似。 换言之除了adb命令外,利用subprocess可以执行其他的命令,比如ls,cd等等...

Global site tag (gtag.js) - Google Analytics