Python common built-in modules

Source: Internet
Author: User

Subprocess Module

python3.5 will use the Subprocess module to interact with the operating system, such as system commands, and he will replace the subprocess encapsulated by the Os.systemos.spawn*subprocess.run () method. The Popen () Subprocess.run () method only in 3.5 has a Subprocess.call () method >>> subprocess.call ([' DF ', '-LH ']) Filesystem     Size   used  avail capacity  iused    ifree%iused  mounted on/dev/disk1 &nbsp ;  465gi  209gi  256gi    45% 54730457 67109157   45%  //dev/disk2s1  741mi  5 57Mi  184mi    76%   142675    47036   75%  /volumes/parallels Desktop 11/dev/dis K3s2  786mi  669mi  117mi    86%   171353    29853   85%  /volumes/para Llelsdesktop 11.1.3-325210 here to the call method, passing "DF-LH" This multiple parameters, with the form of the incoming list [' DF ', '-LH '],python interpreter when handling the Subprocess.call () method, The passed-in list parameters are processed and eventually converted to DF-LH in the shell, so what if I don't want to pass the list? 2.7 can also be implemented, the following methods  >>> subprocess.call (' Df-lh ', shell=true) Filesystem     Size   used   Avail capacity  iused   ifree%iused  mounted on/dev/disk1    465gi  209gi  256gi    45% 54730303 67109311   45%  //dev/disk2s1  741mi  557mi  184mi    76%   142675     47036   75%  /volumes/parallels Desktop 11/dev/disk3s2  786mi  669mi  117mi    86% &NB Sp 171353    29853   85%  /volumes/parallelsdesktop 11.1.3-325210 here Subprocess.call (' Df-lh ', shell= True) tells the Python interpreter explicitly that you don't have to turn it around and use the shell to execute it.   Most of the time, we want to get the execution results from the module method in Python. How to achieve it. >>> a = Subprocess.call ("Df-lh", Shell=true) Filesystem     Size   used  avail capacity   iused    ifree%iused  mounted on/dev/disk1    465gi  209gi  256gi    45% 547 30696 67108918   45%  //dev/disk2s1  741mi  557mi  184mi    76%   142675   & nbsp;47036   75%  /volumes/parallels Desktop 11/dev/dIsk3s2  786mi  669mi  117mi    86%   171353    29853   85%  /VOLUMES/PA Rallelsdesktop 11.1.3-32521>>> A0 Here we see that 0 is the return state of the execution command. So how do we get the results? Through the pipe. Returns the result through a pipeline. >>> a = Subprocess.call ("Df-lh", shell=true,stdout=subprocess. PIPE) >>> A0 We get remained 0, for Mao. Because the call method returns only the execution state. The command execution result is not returned.   If you want to save it, you can't use call, use the Popen method. >>> a = subprocess. Popen (' Df-lh ', shell=true) >>> Filesystem     Size   used  avail capacity  iused   & Nbsp;ifree%iused  mounted on/dev/disk1    465gi  209gi  256gi    45% 54852887 6698672 7   45%  //dev/disk2s1  741mi  557mi  184mi    76%   142675    47036 &N Bsp 75%  /volumes/parallels Desktop 11/dev/disk3s2  786mi  669mi  117mi    86%   171353 &N Bsp  29853   85%  /volumes/parallelsdesktop 11.1.3-32521 >>> a<subprocess. Popen The object at 0x10881cb50> sees this as the Popen method of the OS module. Then we try to a.read () found no Read () method. There is also no A.stdout.read () method in the A.stdout.read () test. Then analyze the execution of Python running Popen method. The Popen () method executes the shell command inside, in fact, Python has opened a sub-process, the result of the child process to return to the Popen () method, need to use the pipeline, the following:>> a = subprocess. Popen (' Df-lh ', shell=true,stdout=subprocess. PIPE) >>> a.returncodea.returncode>>> a.stda.stderr A.stdin  a.stdout>>> A.stdout.read () ' Filesystem     Size   used  avail capacity  iused    ifree%iused &nbsp ; Mounted On\n/dev/disk1    465gi  209gi  255gi    46% 54971946 66867668   45%  /\n /dev/disk2s1  741mi  557mi  184mi    76%   142675    47036   75%  /volu Mes/parallels Desktop 11\n/dev/disk3s2  786mi  669mi  117mi    86%   171353    29 853   85%  /volumes/parallelsdesktop 11.1.3-32521\n ' summary: Use the subprocess module to get command execution results. 1. Use the Popen Method 2. Use pipe 3. Use A.stdout.The read () method remembers the following example:  a = subprocess. Popen (' Df-lh ', shell=true,stdout=subprocess. PIPE)  subprocess Several other methods Subprocess.check_call () Check the execution results if the return 0 instruction is performed normally, if not returned 0, return error >>> Subprocess.call (' Sssdf ', shell=true)/bin/sh:sssdf:command not found127>>> subprocess.check_call (' sssdf ', shell=True)/bin/sh : Sssdf:command not Foundtraceback (most recent):  File "<stdin>", line 1, in <module>  F Ile "/system/library/frameworks/python.framework/versions/2.7/lib/python2.7/subprocess.py", line 540, in Check_call     Raise Calledprocesserror (Retcode, cmd) subprocess. Calledprocesserror:command ' SSSDF ' returned Non-zero exit status 127>>> subprocess.check_stdout ()  subprocess. Popen () Other parameters, remember the following three. Other used  stdin,stdout,stderr: The standard input, output, and error handle of the program, respectively, for example:>>> obj = subprocess. Popen (["Python"],stdin=subprocess. Pipe,stdout=subprocess. Pipe,stderr=subprocess. PIPE) >>> obj.stdin.write (' Print 1 \ n ') >>> obj.stdin.write ('Print 2 \ n ') >>> obj.stdin.write (' Print 3 \ n ') >>> obj.stdin.write (' Print 4 \ n ') to execute here, I want to execute the previous input, Then you need to use Communicate () method >>> out_error_list = Obj.communicate ()               &NBS P                    >>> print out_error_list                              ,         &NB Sp                 (' 1\n2\n3\n4\n ', ')   python2.7 is written in this way, python3.5 do not need to popen, want to command execution results with the Subporcess.run () method, the Run method in 2.7 is not >>> B = subprocess.run (' DF -lh ', shell=true,stdout=subprocess. PIPE) >>> bcompletedprocess (args= ' Df-lh ', returncode=0, Stdout=b ' Filesystem     Size   Used & nbsp Avail capacity  iused    ifree%iused  mounted on\n/dev/disk1    465gi  210gi  25 5Gi    46% 54985500 66854  45%  /\n/dev/disk2s1  741mi  557mi  184mi    76%   142675    4703 6   75%  /volumes/parallels Desktop 11\n/dev/disk3s2  786mi  669mi  117mi    86% &nbsp ; 171353    29853   85%  /volumes/parallelsdesktop 11.1.3-32521\n ') >>>

Python common built-in modules

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.