zoukankan      html  css  js  c++  java
  • commands moudle on python will replace with subprocess on py3.0

    python模块commands的使用
    2009-02-02 12:03

    作者: 李春林
    来源: http://hi.baidu.com/leechl


    平时都是使用Perl较多, 要获得shell命令的输出只需要`cmd`就可以了, 需要得到命令执行的状态则需要判断$?的值, 在Python中有一个模块commands也很容易做到以上的效果.


    看一下三个函数:

    1). commands.getstatusoutput(cmd)

    用os.popen()执行命令cmd, 然后返回两个元素的元组(status, result). cmd执行的方式是{ cmd ; } 2>&1, 这样返回结果里面就会包含标准输出和标准错误.


    2). commands.getoutput(cmd)

    只返回执行的结果, 忽略返回值.


    3). commands.getstatus(file)

    返回ls -ld file执行的结果.


    看一下这些函数使用的例子:
    >>> import commands
    >>> commands.getstatusoutput('ls /bin/ls')
    (0, '/bin/ls')
    >>> commands.getstatusoutput('cat /bin/junk')
    (256, 'cat: /bin/junk: No such file or directory')
    >>> commands.getstatusoutput('/bin/junk')
    (256, 'sh: /bin/junk: not found')
    >>> commands.getoutput('ls /bin/ls')
    '/bin/ls'
    >>> commands.getstatus('/bin/ls')
    '-rwxr-xr-x 1 root        13352 Oct 14 1994 /bin/ls'

    参考文档: http://docs.python.org/library/commands.html
  • 相关阅读:
    极验滑动验证码
    保利威视频播放
    Redis
    DRF 解析器和渲染器
    DRF 分页组件
    DRF 权限和频率
    Nginx反向代理
    Laravel使用反向migrate 和 iseed扩展导出表数据
    psr规范发展历程
    supervisor
  • 原文地址:https://www.cnblogs.com/lexus/p/1833117.html
Copyright © 2011-2022 走看看