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