Using Python call system commands

os.system()

 

The function returns the return value of the command execution result, system () function in the following three steps during execution:

1, fork a child process;

2, call the exec function in the child process to execute the command;

3, in the parent process calls wait (blocking) to wait for the end of the child process.

 

Return 0 indicates that the command is successful, the other for failure.

 

Note: The error occurs often use this function somehow, but the problem did not directly execute the command, it is generally recommended.

 

用法:os.system("command")

 

os.popen()

 

This invocation is achieved by way of the conduit, the function returns the object file read, reads it read, readlines see output and other operations performed.

 

Note: If the command fails, you can not read the contents.


 

用法:os.popen("command")

 

subprocess.Popen()

 

subprocess module is recommended to replace some of the old modules and functions, such as: os.system, os.spawn *, os.popen * etc.

 

subprocess module aims to fork a new process and communicate with, the most commonly used is the definition of class Popen, Popen can use to create processes and complex interactions and processes. Its function prototype is:

 

class subprocess.Popen(args, bufsize=0, executable=None, stdin=None, stdout=None, stderr=None, preexec_fn=None, close_fds=False, shell=False, cwd=None, env=None, universal_newlines=False, startupinfo=None, creationflags=0)


 

args: This parameter must be a string or a string of members of the list. Where if this is a list of strings, then the first member of the path you want to run the program and the program name; starting from the second member to the parameter last member to run the program requires input.

 

executable: Specifies the program to run, this is rarely used, because the program to be run in the specified args has been specified. stdin, stdout, stderr: representing the canonical input, standard output and standard error handling. Value choices are PIPE, open an existing file object and NONE. If stdout is a file object, then the object is to ensure that the file is open.

 

shell: The default is False. shell parameters set according to the command to be executed, if the shell parameter is set to True, executable program will specify the use of shell. In windows platform, the default shell is specified by the COMSPEC environment variable.

 

bufsize: Specifies the buffer. 0 unbuffered, a line buffer, the other buffer size, the buffer system negative

 

cwd: used to set the current directory of the child process

 

stdin, stdout, stderr: represent the standard input, standard output, standard error procedures can be subprocess.PIPE or other programs, files.

 

env: used to specify the child process environment variables. If env = None, environment variables child process inherited from the parent process.

 

universal_newlines: different systems of different line breaks, True ie use \ n

 

preexec_fn: only active in the Unix platform, is used to specify an executable objects (callable object), it will be called before the child process run.


 

用法: child = subprocess.Popen(["cmd","arg1"...])

 

Examples of methods that can be called class subprocess.Popen

 

Popen.poll () is used to check the child process (command) has been performed the end, not the end of the return None, after the return status code.

Popen.wait (timeout = None) wait for the child process to finish, and returns a status code; if the process is not over after a timeout specified number of seconds, TimeoutExpired will throw an exception.

Popen.communicate (input = None, timeout = None) This method can be used to interact with the process, such as sending data to the stdin, stdout and read data from stderr, until it reaches the end of the file.

Popen.send_signal (signal) sends the specified signal to the child process.

Popen.terminate () to stop the child process.

 

Popen.kill () to kill the child process.

 

to sum up

subprocess is used to replace os.system and other functions, when subprocess.call (), subprocess.check_call (), subprocess.check_output () and subprocess.run () function can not meet these high demands, we can use subprocess.Popen classes to implement complex functions we need.

Published 157 original articles · won praise 43 · views 90000 +

Guess you like

Origin blog.csdn.net/qq_39581763/article/details/103816248