36. subprocess Introduction in Python

Source: Internet
Author: User
Tags stdin

Import subprocess

1. Execute system commands
Subprocess.call (' ipconfig ')
#shell =false, the stitching command is written separately, placed in the list, equal to True, can write a piece, space separated
Example:
Subprocess.call ([' ls ', '-l '],shell=false)
Subprocess.call (' ls-l ', shell=true)
‘‘‘
2.check_call
Executes the command, returns 0 if the execution status code is 0, or throws an exception
‘‘‘
#ret =subprocess.check_call (' ipconfig ')
#subprocess. Check_call (' ipconfig ', shell=true)

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

# subprocess.check_output (["echo", "Hello world!"])
# Subprocess.check_output ("Exit 1", shell=true)

‘‘‘
4.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.
‘‘‘
Subprocess. Popen (' mkdir t1 ', shell=true,cwd= ' d:\\1122 ') #先进入D: \\1122, create d:\\1122 folder under T1

36. subprocess Introduction in Python

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.