python 调用linux命令-Python 执行Linux系统命令的N种方法

前言:

很多时候我们会用到python去调用外部工具/命令去实现某种功能。

I. os

https://docs.python.org/2/library/os.html

os.system

执行流程

system(command) -> exit_status

Execute the command (a string) in a subshell.

# os.system() 是新起一个shell去干活的,对系统的开销比较大

# 仅在一个子终端运行系统命令,而不能获取命令执行后的返回信息

# 无法控制,(如果调用的外部命令,挂死或者执行时间很长),主进程无法控制os.system(), 因为调用os.system(cmd) 调用进程会block, until os.system() 自己退出

In [30]: import os

In [31]: os.system('ls *.py')

check_drive_usage.py diff_file.py fcSpider.py fedspider.py get_host_list.py test.py while.py

Out[31]: 0

os.popen

执行流程

popen(command [, mode='r' [, bufsize]]) -> pipe

Open a pipe to/from a command returning a file object.

# 该方法不但执行命令还返回执行后的信息对象

# 好处在于:将返回的结果赋于一变量,便于程序的处理

In [32]: py = os.popen('ls *py').readlines()

In [33]: print py

['check_drive_usage.py ', 'diff_file.py ', 'fcSpider.py ', 'fedspider.py ', 'get_host_list.py ', 'test.py ', 'while.py ']

II. commands

https://docs.python.org/2/library/commands.html

常用的主要有两个方法:getoutput和getstatusoutput

In [40]: import commands

In [41]: commands.getoutput('ls *.py')

Out[41]: 'check_drive_usage.py diff_file.py fcSpider.py fedspider.py get_host_list.py test.py while.py'

In [41]: commands.getstatusoutput('ls *py')

Out[41]:

(0,

'check_drive_usage.py diff_file.py fcSpider.py fedspider.py get_host_list.py test.py while.py')

III. subprocess [ 推荐使用 ]

https://docs.python.org/2/library/subprocess.html

http://python.usyiyi.cn/python_278/library/subprocess.html

# 运用对线程的控制和监控,将返回的结果赋于一变量,便于程序的处理

# 会自动地加载系统环境变量。

subprocess模块主要用于替代以下几个模块函数

os.system

os.spawn*

os.popen*

popen2.*

commands.*

相对应的subprocess 模块里有 call 函数和 popen 函数 。

1、subprocess.call

call 函数的用法如下:

subprocess.call(args, *, stdin=None, stdout=None, stderr=None, shell=False)

可以看出,相对于os模块中的函数,这里可以指定的选项更多。

In [64]: import subprocess

In [65]: subprocess.call('ls *py ',shell=False)

check_drive_usage.py diff_file.py fcSpider.py fedspider.py get_host_list.py test.py while.py

Out[65]: 0

交互式模式下,call 也会有returncode 0 输出,不过在py文件里执行时,ruturn的结果并不会将最后的 0 输出。不过在使用call 函数时,需要注意后面的几个参数:

开启shell=True是不安全的

Using shell=True can be a security hazard. See the warning under Frequently Used Arguments for details.

Note:

尽量不要启用标准输出和标准错误输出需要管道,call有可能会导致子进程死锁。如需管道时,请使用Popen函数

Do not use stdout=PIPE or stderr=PIPE with this function as that can deadlock based on the child process output volume. Use Popen with the communicate() method when you need pipes.

subprocess.call 主要用于替换 os.system ,具体如下:

In [66]: subprocess.call('date')

Thu Oct 29 16:02:24 CST 2015

Out[66]: 0

sub.process.Popen的用法如下:

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)

eg:

In [67]: import subprocess

In [68]: p = subprocess.Popen('ls *.py', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

In [69]: print p.stdout.readlines()

['check_drive_usage.py ', 'diff_file.py ', 'fcSpider.py ', 'fedspider.py ', 'get_host_list.py ', 'test.py ', 'while.py ']

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值