subprocess module of Python important module

Source: Internet
Author: User
Tags string format throw exception

subprocess module of Python important module

We often use Python to execute system commands or scripts, and the system's shell commands are independent of your Python process, and each execution of a command is equivalent to initiating a new process that invokes a system command or script module through Python.

We've also learned about module-os modules that interact with systems.

In [1import osIn [2]: os.system(‘uname -a‘)Linux host-10-200-137-1953.10.0-693.21.1#1 SMP Wed Mar 7 19:03:37 UTC 2018 x86_64 x86_64 x86_64 GNU/LinuxOut[20  # 命令的执行返回结果,0为执行成功,非0表示失败

In addition to So.system can invoke system commands, commands and POPEN2, etc. can also, because of the chaos, so the official launch of the subprocess, the purpose is to provide a unified module to implement the system command or script calls. A simple demonstration of the use of commands:
Commands module Explanation

# commands模块,在python2中运行>>> import commands  # 导入这个模块>>> status,output = commands.getstatusoutput(‘ls‘)  >>> status  # 代表shell命令的返回状态0>>> output  # 代表shell命令你的执行结果‘anaconda-ks.cfg‘
Three ways to execute a command
  • Subprocess.run () # official recommendation
  • Subprocess.call () # The same as the run () method, another way of writing
  • Subprocess. Popen () # Encapsulation of the various methods above
Run () method

Standard notation

subprocess.run([‘df‘,‘-h‘],stderr=subprocess.PIPE,stdout=subprocess.PIPE,check=True)

Slowly speaking:

In [2]: Subprocess.run ([' DF ', '-h ']) # This is the form of splitting a shell command into a list filesystem Size used Avail use% mounted on/dev/m      Apper/centos-root 50G 2.2G 48G 5%/devtmpfs 3.9G 0 3.9G 0%/devtmpfs 3.9G 0 3.9G 0%/dev/shmtmpfs 3.9G 8.4M 3.9G 1%/runtmpfs 3.9G 0 3.9G 0 %/sys/fs/cgroup/dev/vda1 509M 175M 335M 35%/boottmpfs 783M 0 783M 0%/run/us ER/0OUT[2]: completedprocess (args=[' df ', '-h '], returncode=0) # Returns an Object # Assignment a = Subprocess.run ([' DF ', '-h ']) # At this point A is the one that returns Like, through this object, we can use many methods in [3]: A # is the object returned OUT[3]: completedprocess (args=[' df ', '-h '], returncode=0) in [4]: A.returncode # View shell command execution status out[4]: 0In [5]: A.args # View the parameters with Out[5]: [' DF ', '-H ']in [6]: A.check_returncode # To determine whether the command execution status is 0, not 0 error out[6] : <bound method Completedprocess.check_returncode of Completedprocess (args=[' df ', '-h '), returncode=0) >In [7]: A.stdoutin [8]: A.stderr

So the question is, why did A.stdout and a.stderr not output anything? Let's take a look at the working mechanism inside the subprocess module, right?

Through Python to execute the liunx command, the equivalent of launching a new process, we like: In Word document opened QQ, Word to occupy memory, QQ also to occupy memory, then in Word can give QQ friends to send messages?
The answer is not, because: if Wold can go to visit QQ memory data, then you can send data through QQ, then there will be a problem, QQ was blown, so the operating system in order to avoid such things happen, strict restrictions on each process in memory are independent, We make an analogy: in the browser copy and paste, in fact, through the operating system to achieve, the operating system also has its own memory, from the content of the browser assignment, and then through the operating system to QQ, etc., the operating system can access the memory of each process.
Python and shell relationships are like this, Subprocess helped us to launch a process and then get the result, Python and shell is not interoperable, if you want to communicate, then need to have a bridge, that is, Python and the operating system between the bridge.
So there's a way to do that.

subprocess.run([ls‘‘,‘-l‘],stdout=subprocess.PIPE,stderr=subprocess.PIPE)其中PIPE就相当于是桥梁,stdout和stderr被称为标准输出# 此时让我们再看一下运行结果In [9]: a = subprocess.run([‘ls‘,‘-l‘],stdout=subprocess.PIPE,stderr=subprocess.PIPE)In [10]: a.stdout  # 标准输出Out[10]: b‘total 4\n-rw-------. 1 root root 1224 Oct 15  2016 anaconda-ks.cfg\n‘In [11]: a.stderr  # 标准错误输出Out[11]: b‘‘

Go on, what does check=true mean in the Run () method? Write the code to see:

In []: a = Subprocess.run ([' ls ', '-ldada '],stdout=subprocess. Pipe,stderr=subprocess. Pipe,check=true) # intentionally set the shell command is wrong, the result does not check ...: in []: a = Subprocess.run ([' ls ', '-lkjh '],stdout=subprocess. Pipe,stderr=subprocess. pipe,check=true) # Check if the shell command is correct,                        Incorrect then error---------------------------------------------------------------------------Calledprocesserror Traceback (most recent) <ipython-input-14-0e63d8200326> in <module> ()----> 1 a = Subprocess.run ( [' ls ', '-lkjh '],stdout=subprocess. Pipe,stderr=subprocess.         Pipe,check=true)/usr/local/lib/python3.6/subprocess.py in run (input, timeout, check, *popenargs, **kwargs) 416                                      If check and retcode:417 raise Calledprocesserror (Retcode, Process.args,--> 418 Output=stdout, Stderr=stderr) 419 return completedprocess (Process.args, Retcode, stdout, stderr) 4 Calledprocesserror:command ' [' ls ', '-lkjh '] ' returned Non-zero exit status 2.In []: a = Subprocess.run ([' ls ', '-lkjh '],stdout=subprocess. Pipe,stderr=subprocess. PIPE) in [+]: a = Subprocess.run ([' ls ', '-lkjh '],stdout=subprocess. Pipe,stderr=subprocess. PIPE), check=true File "<ipython-input-16-15adea8e8d2f>", line 1 a = Subprocess.run ([' ls ', '-LKJH '],stdout=subpro Cess. Pipe,stderr=subprocess. PIPE), check=true ^syntaxerror:can ' t assign to function call

If you want to execute complex shell commands, let's take a look at how to write them.

In [17]: a = subprocess.run([‘df‘,‘-h‘,‘|‘,‘grep‘,‘Used‘],stdout=subprocess.PIPE,stderr=subprocess.PI    ...: PE)  # 刚刚的ls -l不就是这样的嘛一直拼参数怎么会报错了呢In [18]: a.stderr  # 我知道是错误的,故意直接写的是标准错误输出Out[18]: b‘df: \xe2\x80\x98|\xe2\x80\x99: No such file or directory\ndf: \xe2\x80\x98grep\xe2\x80\x99: No such file or directory\ndf: \xe2\x80\x98Used\xe2\x80\x99: No such file or directory\n‘# 其实应该这样写In [21]: a = subprocess.run(‘df -h | grep Used‘,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)In [22]: a.stdoutOut[22]: b‘Filesystem               Size  Used Avail Use% Mounted on\n‘In [23]: a.stderrOut[23]: b‘‘# shell=True这样写就是告诉subprocess,你之前帮我拼参数,但是现在不用了,只需要把整条命令传递个shell去处理就行了
Call () method

Not much nonsense to say, look directly at the code

In []: Subprocess.call ([' ls ', '-l ']) total 4-RW-------. 1 root root 1224 Oct anaconda-ks.cfgout[24]: 0 # The return status of the command execution, 0 for Success # Execution command, if the command result is 0, return normally, otherwise throw exception in []: SUBPROCESS.C Heck_call ([' ls ', '-l ']) total 4-RW-------.  1 root root 1224 Oct anaconda-ks.cfgout[25]: 0In [+]: subprocess.check_call ([' ls ', '-lhgj ']) ls:invalid option-- ' j ' Try ' ls--help ' for more                        Information.---------------------------------------------------------------------------Calledprocesserror Traceback (most recent) <ipython-input-26-c38033ac38dc> in <module> ()----> 1 Subprocess.che  Ck_call ([' ls ', '-lhgj '])/usr/local/lib/python3.6/subprocess.py in Check_call (*popenargs, **kwargs) 289 if CMD is    none:290 cmd = popenargs[0]--> 291 Raise Calledprocesserror (Retcode, cmd) 292 return 0 293 Calledprocesserror:command ' [' ls ', '-lhgj '] ' returned Non-zero exit status ' 2.# accepts a string format command, returns a tuple form, the 1th element is the execution state, and the 2nd is the command results in [+]: subprocesS.getstatusoutput (' Ls/bin/ls ') out[27]: (0, '/bin/ls ') # receives the string format command and returns the result in []: Subprocess.getoutput (' df-h | grep used ') ) out[29]: ' Filesystem Size used Avail use% mounted on ' # executes the command and returns the result, note that the result is returned, not printed,
Popen () method

Common parameters

  • Args:shell command, which can be either a character or a serialized type (list,tuple)
  • Stdint,stdout,stderr: Indicates the standard input, output, error of the program, respectively
  • PREEXEC_FN: Valid only on UNIX platforms, used to specify an executable object that will be called before the child process is run
  • Shell: Ibid.
  • CWD: Used to specify the current directory of child processes
  • ENV: The environment variable used to specify the child process, and if env=none, the environment variable of the child process will inherit from the parent process

What is the difference between the following two statements execution?

In [7]: a = subprocess.call(‘sleep 10‘,shell=True,stdout=subprocess.PIPE)  # 它会一直等待着这段代码执行完才结束In [8]: a = subprocess.Popen(‘sleep 10‘,shell=True,stdout=subprocess.PIPE)  # 相当于是有两个进程,主程序继续下一步行动,其余则另开一个新的进程执行sleep 10

If you call the command or script to execute 10 minutes, your main program does not need to wait for 10 minutes of the card, you can continue to do something else, every once in a while, you can use the poll () method to detect whether the command is completed.
The Popen call returns an object that can be used to get the execution result or state of the command, and the object has the following methods:
1. poll() : Check the execution status of this process, do not output, execute the correct output 0, error not 0

In [13]: a = subprocess.Popen(‘sleep 10‘,shell=True,stdout=subprocess.PIPE)In [14]: a.poll()

2. wait() : Wait for the process to end and know to return to run result

In [15]: a = subprocess.Popen(‘sleep 10‘,shell=True,stdout=subprocess.PIPE)In [16]: a.wait()Out[16]: 0

3. terminate() : Terminate the process that is started
4. kill() : Kill the process that was started

In [24]: a = subprocess.Popen(‘for i in $(seq 1 100);do sleep 1;echo $i >> sleep1.log;done‘,shell=True,stdou    ...: t=subprocess.PIPE)In [25]: a.terminate  # 这个是内存地址Out[25]: <bound method Popen.terminate of <subprocess.Popen object at 0x7f49d85717f0>>In [26]: a.terminate()  # 杀死进程的一种方法In [28]: a = subprocess.Popen(‘for i in $(seq 1 100);do sleep 1;echo $i >> sleep1.log;done‘,shell=True,stdou    ...: t=subprocess.PIPE)In [29]: a.kill()  # 杀死进程的另一种方法

5. pid() : Gets the PID of the child process

In [30]: a = subprocess.Popen(‘for i in $(seq 1 100);do sleep 1;echo $i >> sleep1.log;done‘,shell=True,stdou    ...: t=subprocess.PIPE)In [33]: a.pidOut[33]: 4931

Now let's talk about some of the parameters of the Popen method

# cwd设置子进程的当前目录In [4]: a = subprocess.Popen(‘echo $PWD;sleep 2‘,shell=True,cwd=‘/tmp‘,stdout=subprocess.PIPE)In [5]: a.stdout.read()Out[5]: b‘/tmp\n‘# preexec_fn=  在子进程运行之前被调用# 最后一个,与程序的交互,communicate(),发送数据到stdin,并从stdout接收输入,然后等待任务结束这是一个猜数字的游戏1.py 11 import random 12       13 num = random.randint(1,100) 14 your_guess = int(input(‘your_guess:‘)) 15       16 if your_guess > num: 17     print(‘bigger‘) 18 elif yur_guess < num: 19     print(‘smaller‘) 20 else: 21     print(‘ok‘)   程序交互:>>> import subprocess>>> a = subprocess.Popen(‘python3 1.py‘,shell=True,stdout=subprocess.PIPE)然后:a.communicate(b‘22‘)  # 就可以了,我的xshell一运行就断开连接,所以,我就不演示啦。

subprocess module of Python important module

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.