zoukankan      html  css  js  c++  java
  • Python执行系统命令的方法 os.system(),os.popen(),commands

    #os.system() 无法获取输出和返回值。
    os.system("ipconfig /all")
    
    
    #os.popen()返回值是file read 对象,对其进行读取read()操作可以看到执行的输出
    output = os.popen('ipconfig /all')
    print output.read()
    
    #commands.getstatusoutput() 一个方法就可以获得到返回值和输出
    import commands
    (status, output) = commands.getstatusoutput("ipconfig /all")
    print status, output
    

    Python Document 中给的一个例子,很清楚的给出了各方法的返回。

    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'
    
  • 相关阅读:
    【情感计算】资源
    有意思的网站
    【NLP】新词发现
    [openwrt]网络配置
    【SSH】远程下载
    【OpenWRT】网络配置
    [SSH]连接失败
    [Openwrt]wifi桥接设置
    【Openwrt】刷
    【Python】excel
  • 原文地址:https://www.cnblogs.com/xiyuan2016/p/7119884.html
Copyright © 2011-2022 走看看