zoukankan      html  css  js  c++  java
  • python模块之subprocess

    import subprocess
    subprocess.run("ipconfig")
    
    
    # subprocess.run("ifconfig /all")
    subprocess.run(["ipconfig","/all"])
    
    
    # subprocess.run("df")
    # 这样是可以的
    
    # subprocess.run("df -h")
    # 如果带一个参数就不可以了
    
    # subprocess.run(["df","-h"])
    # 带一个参数可以这样执行
    # subprocess.run("df -h",shell=True)
    # 带一个参数也可以这样执行,使用原生的shell
    
    
    #这样是接受不到ipconfig这个命令的返回结果的,这里的a是命令执行的状态,而不是结果
    a = subprocess.run("ipconfig")
    print(a)
    
    
    #subprocess的结果可以通过下面的方法来接受到,Popen是获取命令的执行的结果,因为命令是在新的进程中执行的,所以这里这里需要用管道
    #把命令的结果传递给当前的进程,然后才能Popen来接受到命令的结果
    a = subprocess.Popen("ipconfig",stdout=subprocess.PIPE)
    print(a.stdout.read())
    
    
    
    
    
    # a = subprocess.Popen("df -h",shell=True,stdout=subprocess.PIPE)
    # print(a.stdout.read())
    
    
    
    #check_call方法,如果命令执行失败,则抛出异常,如果执行成功,则返回0
    
    # a = subprocess.check_call("aaa")
    # print(a)
    #
    # a = subprocess.check_call("ipconfig")
    # print(a)
    
    #如果命令执行成功,则返回结果,如果命令执行失败,则抛出异常
    # subprocess.check_output()
    
    # subprocess.check_output("ipconfig")
    # subprocess.check_output("ifconfig")
    
    
    
    
    # a = subprocess.Popen(["python"],stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
    #
    # a.stdin.write("print('hello')
    ")
    # a.communicate()
    

      

  • 相关阅读:
    初入水:vector
    Sort Colors
    Palindrome Partitioning II
    Search for a Range
    Container With Most Water
    Palindrome Partitioning
    Longest Consecutive Sequence
    简单写了一个堆排序
    Best Time to Buy and Sell Stock III
    4-7
  • 原文地址:https://www.cnblogs.com/bainianminguo/p/7215744.html
Copyright © 2011-2022 走看看