zoukankan      html  css  js  c++  java
  • 模块讲解----subprocess模块

    历史

    #输出结果到屏幕上,并不返回执行状态
    os.system('dir')
    #保存命令的执行结果输出
    ret = os.popen('dir').read()

    问题:上面2条是把命令结果保存下来了,但是返回状态没了,也就没办法判断该命令是否执行成功。例如:

    解决方式: 既想要执行结果邮箱要执行状态请使用subprocess模块。

     注意:
    在python2.7里有个commands模块,可以通过commands.getstatusoutput("dir")查看系统命令的执行状态,但是在3.5里就已经弃用了。
    在3.5里新出了一个subprocess模块来替换os.system、os.spawn*、 commands等模块的功能。

    subprocess模块:

    作用:

    1、只获取系统的状态码;
    2、只获取shell命令执行后的内容;
    3、可以通过Popen来设置一个路径来增删改查文件的操作;
    4、可以通过交互式的方式来添加和写入内容操作;
     
     
    在python3.5以后的版本可以使用run,之前版本没有run方法:
    1 #python解析shell命令:
    2 subprocess.run(["df","-h"])
    3 
    4 #不用python解析shell命令:(shell=True)
    5 subprocess.run("df -h |grep sda1",shell=True)

    一、执行命令,返回状态值,不返回结果:(call)

    1 1、列表形式(shell = False)
    2 ret = subprocess.call(["ls","-l"],shell=False)
    3 print(ret)
    4 
    5 2、字符串形式(shell=True)
    6 ret = subprocess.call("ls -l",shell=True)
    7 print(ret)

    二、执行命令,如果状态码是0,则返回0,否则报异常,不反悔结果:(check_call)

    1 1、列表形式(shell = False)
    2 ret = subprocess.check_call(["ls","-l"],shell=False)
    3 print(ret)
    4 
    5 2、字符串形式(shell=True)
    6 ret = subprocess.check_call("ls -l",shell=True)
    7 print(ret)

    三、执行命令,如果状态码是 0 ,则返回执行结果,否则抛异常:(check_output)

    1 1、列表形式(shell = False)
    2 ret = subprocess.check_output(["echo","hello world"],shell=False)
    3 print(ret)
    4 
    5 2、字符串形式(shell=True)
    6 ret = subprocess.check_output("exit 1",shell=True)
    7 print(ret)

    四、本地创建目录:

    使用subprocess.Popen(...) 用于执行复杂的系统命令(上面的方法在后台实际就是调用的Popen)

    (一)、参数:
    1、args:shell命令,可以是字符串或者序列类型(如:list,元组)
    2、bufsize:指定缓冲。0 无缓冲,1 行缓冲,其他 缓冲区大小,负值 系统缓冲
    3、stdin, stdout, stderr:分别表示程序的标准输入、输出、错误句柄
    4、preexec_fn:只在Unix平台下有效,用于指定一个可执行对象(callable object),它将在子进程运行之前被调用
    5、close_sfs:在windows平台下,如果close_fds被设置为True,则新创建的子进程将不会继承父进程的输入、输出、错误管道。所以不能将close_fds设置为True同时重定向子进程的标准输入、输出与错误(stdin, stdout, stderr)。
    6、shell:同上
    7、cwd:用于设置子进程的当前目录
    8、env:用于指定子进程的环境变量。如果env = None,子进程的环境变量将从父进程中继承。
    9、universal_newlines:不同系统的换行符不同,True -> 同意使用
    10、startupinfo与createionflags只在windows下有效将被传递给底层的CreateProcess()函数,用于设置子进程的一些属性,如:主窗口的外观,进程的优先级等等 。

    (二)、输入即可得到输出,如:ifconfig

    1 import subprocess
    2 ret1 = subprocess.Popen(["mkdir","t1"])
    3 ret2 = subprocess.Popen("mkdir t2", shell=True)

    五、跳转到指定目录然后在创建目录:

    输入进行某环境,依赖再输入,如:python

     1 obj = subprocess.Popen("mkdir t3", shell=True, cwd='/home/dev',) 

    六、通过管道进入交互方式:

     1 import subprocess
     2 
     3 #下面的stdin,stdout,stderr相当于三个管道,以后要想写内容的时候通过管道就可以写。
     4 obj = subprocess.Popen(["python"],  #写了一个python,就会进入python解释器,obj就相当于定义一个对象。
     5          stdin=subprocess.PIPE,      #专门写的管道1
     6          stdout=subprocess.PIPE,    #那正常结果的管道2
     7          stderr=subprocess.PIPE,     #用来拿错误的管道3
     8          universal_newlines=True)
     9 
    10 #向管道里面写数据,下面2个print就相当于写了2行数据。(相当于在终端把命令输入进去了,剩下的就是拿结果)
    11 obj.stdin.write("print(1)
    ")   
    12 obj.stdin.write("print(2)")
    13 obj.stdin.close()   #表示停止写入
    14 
    15 #可以到stdout的管道中取结果,读到正常输出的结果值
    16 cmd_out = obj.stdout.read()
    17 obj.stdout.close()
    18 
    19 #如果出现错误,可以到stderr中提取报错结果
    20 cmd_error = obj.stderr.read()
    21 obj.stderr.close()
    22 
    23 
    24 #最后输出执行命令结果
    25 print(cmd_out)
    26 print(cmd_error)

    七、合并stdout和stderr的输入结果值:(communicate):

     1 import subprocess
     2 
     3 obj = subprocess.Popen(["python"], 
     4          stdin=subprocess.PIPE, 
     5          stdout=subprocess.PIPE, 
     6          stderr=subprocess.PIPE, 
     7          universal_newlines=True)
     8 obj.stdin.write("print(1)
    ")
     9 obj.stdin.write("print(2)")
    10 
    11 out_error_list = obj.communicate()  #其实这个命令就是去stdout和stderr两个管道里去拿返回out和error值,不用再次编辑。
    12 print(out_error_list)

    八、执行简单的命令:

    1 import subprocess
    2 obj = subprocess.Popen(["python"], 
    3          stdin=subprocess.PIPE, 
    4         stdout=subprocess.PIPE, 
    5         stderr=subprocess.PIPE, 
    6         universal_newlines=True)
    7 out_error_list = obj.communicate('print("hello")')
    8 print(out_error_list)
  • 相关阅读:
    白雪公主与七个爷爷——冲刺日志(第二天)
    vba实践
    微信小程序scroll-view不起作用,bindscrolltolower事件无响应
    git bash相关命令
    hexo博客中命令备忘(markdown语法)
    微信聊天记录生成词云图(等日后有机会补充完整)
    微信小程序解决存在图片太大而有效显示区域较小--阿里云oss产生缩略图
    css样式基础库--global.css(持续更新中~)
    Chrome控制台常用技巧
    css基础-动画效果
  • 原文地址:https://www.cnblogs.com/abobo/p/8109363.html
Copyright © 2011-2022 走看看