Python Basic Learning Log day5--subprocess module

Source: Internet
Author: User

The relevant modules and functions that can execute shell commands are:

    • Os.system
    • os.spawn*
    • os.popen*--Waste
    • popen2.*--Waste
    • commands.*-Discarded, removed in 3.x

The functions of the relevant modules and functions for executing shell commands are implemented in the Subprocess module and provide richer functionality

Pager

Parent process waits for child process to complete
Return exit information (ReturnCode, equivalent to Linux exit code)

Executes a command, returns a status code, Shell=true is a string that indicates that the command can

>>>Importsubprocess>>> Ret=subprocess.call (["ls","- L"],shell= False) Total4-rw-r--r--. 1 root root 260 Feb 23 20:44SCRAPY.CFGDRWXR-xr-x. 4 root root 156 Feb 25 20:05Sokindle>>> Ret=subprocess.call ("ls-l",shell= True) Total4-rw-r--r--. 1 root root 260 Feb 23 20:44SCRAPY.CFGDRWXR-xr-x. 4 root root 156 Feb 20:05 Sokindle

Check_call

Parent process waits for child process to complete
Returns 0
Check the exit information, and if ReturnCode is not 0, cite error subprocess. Calledprocesserror, this object contains the ReturnCode property, which can be try...except ... To check

The shell defaults to False, under Linux, when Shell=false, Popen calls OS.EXECVP () to execute the program specified by args, or shell=true, if args is a string, Popen directly invokes the system's shell to execute the program specified by args, and if args is a sequence, the first item of args is the definition of the program command string, and the other is additional parameters when invoking the system shell.

>>> Ret=subprocess.check_call (["ls","- L"]) Total4-rw-r--r--. 1 root root 260 Feb 23 20:44SCRAPY.CFGDRWXR-xr-x. 4 root root 156 Feb 25 20:05Sokindle>>>Print(ret) 0>>>Subprocess.check_call ("exit 1", shell= True) Traceback (most recent): File"<stdin>", Line 1,inch<module>File"/opt/python36/lib/python3.6/subprocess.py", line 291,inchCheck_callRaisecalledprocesserror (retcode, cmd) subprocess. Calledprocesserror:command'Exit 1'Returned Non-zero exit status 1.>>>Subprocess.call ("exit 1", shell=True)1>>>

Check_output, similar to Check_call

Executes the command, if the status code is 0, returns the execution result, otherwise throws an exception

Subprocess. Popen (...)

Used to perform complex system commands

Parameters:

      • The Args:shell command, which can be a string or sequence type (for example: list, tuple)
      • BufSize: Specifies buffering. 0 unbuffered, 1 row buffer, other buffer size, negative system buffering
      • stdin, stdout, stderr: Indicates the standard input, output, and error handles of the program, respectively
      • PREEXEC_FN: Valid only on UNIX platforms, specifying an executable object (callable object) that will be called before the child process is run
      • CLOSE_SFS: Under the Windows platform, if Close_fds is set to true, the newly created child process will not inherit the input, output, and error pipes of the parent process.
        Therefore, you cannot set Close_fds to true while redirecting the standard input, output, and error (stdin, stdout, stderr) of the child process.
      • Shell: Ibid.
      • CWD: Used to set the current directory of child processes
      • ENV: The environment variable used to specify the child process. If env = None, the environment variables of the child process are inherited from the parent process.
      • Universal_newlines: Different system line breaks differ, True----agree to use \ n
      • Startupinfo and Createionflags are only valid under Windows
        will be passed to the underlying CreateProcess () function to set some properties of the child process, such as: the appearance of the main window, the priority of the process, etc.
>>>Importsubprocess>>> subprocess. Popen (["mkdir","T1"])    <subprocess. Popen object at 0x7f7419f0fac8>>>> ret1=subprocess. Popen (["mkdir","T1"])>>>mkdir:cannot Create directory ' T1 ': File exists>>> ret2=subprocess. Popen ("mkdir T2", shell=True)>>> ret2=subprocess. Popen ("ls-l", shell=True)>>> Total 4-rw-r--r--. 1 root root 260 Feb 23 20:44SCRAPY.CFGDRWXR-xr-x. 4 root root 156 Feb 25 20:05SOKINDLEDRWXR-xr-x. 2 root root 6 May 25 21:29T1DRWXR-xr-x. 2 root root 6 May 21:30 T2
>>>obj = subprocess. Popen ("mkdir T3", Shell=true, cwd='/home/',)          >>> obj = subprocess. Popen ("ls-l", shell=true,cwd=' /Home',)      >>> Total 23464drwxr-xr-x. 5 root root 5 17:18CRAWLDRWXR-xr-x. 3 root root 19 11:40DOUBANDRWXR-xr-x. 3 root-root 1-20:52DOUBAN2DRWXR-xr-x. 5 root root 159 17 2016FEATURESDRWXR-xr-x. 3 root root 25 19:52imagesdrwx------. LX lx 4096 Mar 2 20:59LXDRWXR-xr-x. Root root 4096 17 2016Plugins-rw-r--r--. 1 root root 24015561 Mar 7 20:53 pydev_5.2.0. ZIPDRWXR-xr-x. 4 root root 13 21:52PYTHONDRWXR-xr-x. 2 root root 6 Feb 16 20:22PYTHON36DRWXR-xr-x. 5 root root 25 21:30SOKINDLEDRWXR-xr-x. 2 root root 6 May 25 21:36T3DRWXR-xr-x. 3 root root 122 Feb 21:13 Tutorial

Python Basic Learning Log day5--subprocess module

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.