zoukankan      html  css  js  c++  java
  • python 常用代码学习笔记之commands模块

    通常我们调用os.system(cmd) 只能获得命令是否能执行成功。即结果为0或者非0标识是否执行成功。
    而有时我们希望即获取到是否成功,同时也获取命令的执行结果。
    这时就可以使用commands了,通过它可以同时获取命令的执行结果输出和结果。
    实例如下:
       1: import commands
       2:  
       3: ret, output = commands.getstatusoutput('ls')
       4: print ret 
       5: print output  
     
    这样ret就反馈是否执行成功,比如为0(成功) 或者非0(不成功)
    output 用来获取ls命令的执行结果。
     
    注:查看python api文档:
    commands.getstatusoutput(cmd)

    Execute the string cmd in a shell with os.popen() and return a 2-tuple (status, output)cmd is actually run as { cmd ; } 2>&1, so that the returned output will contain output or error messages. A trailing newline is stripped from the output. The exit status for the command can be interpreted according to the rules for the C function wait().

    注意该命令会将错误输出流重定向到标准输出流中,因此output也会保存错误输出。

  • 相关阅读:
    11.分类与监督学习,朴素贝叶斯分类算法
    14 深度学习-卷积
    13-垃圾邮件分类2
    12.朴素贝叶斯-垃圾邮件分类
    9、主成分分析
    8、特征选择
    7.逻辑回归实践
    6.逻辑归回
    5.线性回归算法
    15 手写数字识别-小数据集
  • 原文地址:https://www.cnblogs.com/lovemdx/p/2950301.html
Copyright © 2011-2022 走看看