Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use subprocess Module in Python

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you how to use the subprocess module in Python, the content is concise and easy to understand, it can definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Subprocess is a new module in Python 2.4.It allows you to generate new processes, connect to their input/output/error pipes, and get their return (status) codes. The purpose of this module is to replace several old modules and methods, such as:

Os.system os.spawn*

The common functions in the subprocess module describe the new functions in subprocess.run () Python 3.5. Executes the specified command and waits for the command to complete and returns an instance of the CompletedProcess class that contains the execution results. Subprocess.call () executes the specified command and returns the command execution status, which is similar to os.system (cmd). A new function in subprocess.check_call () Python 2.5. Executes the specified command and returns a status code if it is successful, otherwise an exception is thrown. Its function is equivalent to subprocess.run (..., check=True). A new function in subprocess.check_output () Python 2.7. Executes the specified command and returns the command execution result if the execution status code is 0, otherwise an exception is thrown. Subprocess.getoutput (cmd) receives a command in string format, executes the command, and returns the execution result, which is similar to os.popen (cmd). Read () and commands.getoutput (cmd). Subprocess.getstatusoutput (cmd) executes the cmd command and returns a tuple (command execution status, command execution result output), which functions similar to commands.getstatusoutput (). Description:

In versions after Python 3.5, the official documentation advocates using the subprocess.run () function instead of other functions to use the functions of the subproccess module; in versions prior to Python 3.5, we can use the functions of the subprocess module through subprocess.call (), subprocess.getoutput () and other functions listed above Subprocess.run (), subprocess.call (), subprocess.check_call (), and subprocess.check_output () are all high-level functions that are encapsulated by subprocess.Popen, so if we need more complex functionality, we can do it through subprocess.Popen. The subprocess.getoutput () and subprocess.getstatusoutput () functions are two legacy functions from the commands module of Python 2.x. They implicitly call the system shell and do not guarantee the security of other functions and the consistency of exception handling. In addition, they do not support the Windows platform until Python 3.3.4. two。 The definition and parameter description of the above functions: copy the code subprocess.run (args, *, stdin=None, input=None, stdout=None, stderr=None, shell=False, timeout=None, check=False, universal_newlines=False)

Subprocess.call (args, *, stdin=None, stdout=None, stderr=None, shell=False, timeout=None)

Subprocess.check_call (args, *, stdin=None, stdout=None, stderr=None, shell=False, timeout=None)

Subprocess.check_output (args, *, stdin=None, stderr=None, shell=False, universal_newlines=False, timeout=None)

Subprocess.getstatusoutput (cmd)

Subprocess.getoutput (cmd) copy code

Parameter description: args: the default shell command to be executed should be a sequence of strings, such as ['df','-Th'] or ('df','-Th'), or a string, such as'df-Th',. At this point, you need to set the value of the shell parameter to True. Shell: if shell is True, the specified command will be executed through shell. This is useful if we need to access some shell features, such as pipes, filename wildcards, and environment variable extensions. Of course, python itself provides implementations of many shell-like features, such as glob, fnmatch, os.walk (), os.path.expandvars (), os.expanduser (), and shutil. Check: if the value of the check parameter is True and the process executing the command exits with a non-zero status code, an exception of CalledProcessError will be thrown, and the exception object will contain the parameter, exit status code, and stdout and stderr, if any. Stdout, stderr:input: this parameter is passed to Popen.communicate (), usually the value of this parameter must be a sequence of bytes, if universal_newlines=True, its value should be a string. The run () function does not capture the normal output and error output of the command execution result by default. If we need to pass subprocess.PIPE to get these contents, we can then capture the corresponding content through the stdout and stderr properties of the returned CompletedProcess class instance. The call () and check_call () functions return the status code of the command execution, not the CompletedProcess class instance, so stdout and stderr are not suitable for assigning to subprocess.PIPE. The check_output () function returns the command execution result by default, so instead of setting the value of stdout, we can execute stderr=subprocess.STDOUT if we want to capture the error message in the result. Universal_newlines: this parameter affects the data format of input and output. For example, if its value defaults to False, the output of stdout and stderr is a sequence of bytes; when the value of this parameter is set to True, the output of stdout and stderr is a string. 3. The introduction of the subprocess.CompletedProcess class needs to note that the subprocess.run () function is a new high-level function in Python3.5, and its return value is an instance of the subprocess.CompletedPorcess class. Therefore, the subprocess. CompletedPortal class also exists in Python3.5. It represents the status information of a finished process and contains the following properties:

Args: the parameter used to load the process, which could be a list or a string returncode: the exit status code of the child process. Typically, an exit status code of 0 indicates that the process has run successfully; a negative value of-N indicates that the child process has been terminated by signal N stdout: the stdout captured from the child process. This is usually a sequence of bytes, and if the universal_newlines=True is specified when the run () function is called, the attribute value is a string. If stderr=subprocess.STDOUT is specified when the run () function is called, then stdout and stderr will be integrated into this property, and stderr will be None stderr: stderr captured from the child process. Its value, like stdout, is a sequence of bytes or a string. If stderr is caught, its value is None check_returncode (): if returncode is a non-zero value, the method throws a CalledProcessError exception. 4. Instance subprocess.run () copies the code

Subprocess.run (["ls", "- l"]) # doesn't capture output CompletedProcess (args= ['ls','-l'], returncode=0)

Subprocess.run ("exit 1", shell=True, check=True) Traceback (most recent call last):... Subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

Subprocess.run (["ls", "- l", "/ dev/null"], stdout=subprocess.PIPE) CompletedProcess (args= ['ls','-lumped,'/ dev/null'], returncode=0, stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 / dev/null\ n') copy the code

Subprocess.call () copy the code

Subprocess.call (['ls'] Total amount: 160 drwxr-xr-x 2 wader wader 4096 December 7 2015 Public drwxr-xr-x 2 wader wader 4096 December 7 2015 template drwxr-xr-x 2 wader wader 4096 December 7 2015 Video drwxr-xr-x 2 wader wader 4096 December 7 2015 Picture drwxr-xr-x 2 wader wader 4096 December 7 2015 document drwxr-xr-x 2 wader wader 4096 April 13, 2016 download drwxr-xr-x 2 wader wader 4096 December 7 2015 Music drwxr- Xr-x 7 wader wader 4096 May 26 2016 Desktop 0 subprocess.call ('ls-l' Shell=True) Total usage 160 drwxr-xr-x 2 wader wader 4096 December 7 2015 Public drwxr-xr-x 2 wader wader 4096 December 7 2015 template drwxr-xr-x 2 wader wader 4096 December 7 2015 Video drwxr-xr-x 2 wader wader 4096 December 7 2015 Picture drwxr-xr-x 2 wader wader 4096 December 7 2015 document drwxr-xr-x 2 wader wader 4096 April 13, 2016 download drwxr-xr-x 2 wader wader 4096 December 7 2015 Music drwxr-xr-x 7 wader Wader 4096 May 26 2016 Desktop 0 subprocess.call (['ls' '- l'], stdout=subprocess.DEVNULL) 0 subprocess.call ([' ls','- lumped,'/ test']) ls: unable to access / test: there is no file or directory 2 to copy the code

Suprocess.check_call () copy the code

Subprocess.check_call (['ls'] Total amount: 160 drwxr-xr-x 2 wader wader 4096 December 7 2015 Public drwxr-xr-x 2 wader wader 4096 December 7 2015 template drwxr-xr-x 2 wader wader 4096 December 7 2015 Video drwxr-xr-x 2 wader wader 4096 December 7 2015 Picture drwxr-xr-x 2 wader wader 4096 December 7 2015 document drwxr-xr-x 2 wader wader 4096 April 13, 2016 download drwxr-xr-x 2 wader wader 4096 December 7 2015 Music drwxr- Xr-x 7 wader wader 4096 May 26 2016 Desktop 0 subprocess.check_call ('ls-l' Shell=True) Total usage 160 drwxr-xr-x 2 wader wader 4096 December 7 2015 Public drwxr-xr-x 2 wader wader 4096 December 7 2015 template drwxr-xr-x 2 wader wader 4096 December 7 2015 Video drwxr-xr-x 2 wader wader 4096 December 7 2015 Picture drwxr-xr-x 2 wader wader 4096 December 7 2015 document drwxr-xr-x 2 wader wader 4096 April 13, 2016 download drwxr-xr-x 2 wader wader 4096 December 7 2015 Music drwxr-xr-x 7 wader Wader 4096 May 26 2016 Desktop 0 subprocess.check_call ('ls-l / test' Shell=True) ls: unable to access / test: no file or directory Traceback (most recent call last): File ", line 1, in File" / usr/lib/python3.4/subprocess.py ", line 557, in check_call raise CalledProcessError (retcode, cmd) subprocess.CalledProcessError: Command'ls-l / test' returned non-zero exit status 2 copy code

Sbuprocess.check_output () copy the code

Ret = subprocess.check_output (['ls' '- l']) print (ret) b'\ xe5\ x85\ xac\ xe5\ X85\ xb1\ xe7\ x9a\ x84\ ndrwxr-xr-x 2 wader wader 4096 12\ xe6\ x9c\ x88 7 2015\ xe6\ xa8\ xa1\ xe6\ x9d\ xbf\ ndrwxr-xr-x 2 wader wader 4096 12\ xe6\ x9c\ x88 7 2015\ xe8\ x86\ xe9\ xa2\ x91\ ndrwxr-xr-x 2 wader wader 4096 12\ xe6\ x9c\ x88 7 2015\ xe5\ x9b\ xbe\ Xe7\ x89\ x87\ ndrwxr-xr-x 2 wader wader 4096 12\ xe6\ x9c\ x88 7 2015\ xe6\ x96\ x87\ xe6\ xa1\ xa3\ ndrwxr-xr-x 2 wader wader 4096 4\ xe6\ x9c\ x88 13 2016\ xe4\ xb8\ X8b\ xe8\ xbd\ xbd\ ndrwxr-xr-x 2 wader wader 4096 12\ xe6\ x9c\ x88 7 2015\ xe9\ X9f\ xb3\ xb9\ X90\ ndrwxr-xr-x 7 wader wader 4096 5\ xe6\ x9c\ x88 26\ xe6\ Xa1\ x8c\ xe9\ x9d\ xa2\ n 'ret = subprocess.check_output ([' ls' '- l'] Universal_newlines=True) print (ret) Total usage 160 drwxr-xr-x 2 wader wader 4096 December 7 2015 Public drwxr-xr-x 2 wader wader 4096 December 7 2015 template drwxr-xr-x 2 wader wader 4096 December 7 2015 Video drwxr-xr-x 2 wader wader 4096 December 7 2015 Picture drwxr-xr-x 2 wader wader 4096 December 7 2015 document drwxr-xr-x 2 wader wader 4096 April 13, 2016 download drwxr-xr-x 2 wader wader 4096 December 7 2015 Music drwxr -xr-x 7 wader wader 4096 May 26 2016 Desktop copy Code

Subprocess.getoutput () and subprocess.getstatusoutput () copy the code

Ret = subprocess.getoutput ('ls-l') print (ret) Total consumption 160 drwxr-xr-x 2 wader wader 4096 December 7 2015 Public drwxr-xr-x 2 wader wader 4096 December 7 2015 template drwxr-xr-x 2 wader wader 4096 December 7 2015 Video drwxr-xr-x 2 wader wader 4096 December 7 2015 pictures drwxr-xr-x 2 wader wader 4096 December 7 2015 document drwxr-xr-x 2 wader wader 4096 April 13 2016 download drwxr-xr-x 2 wader wader 4096 December 7 2015 Music drwxr-xr-x 7 wader wader 4096 May 26 2016 Desktop retcode Output = subprocess.getstatusoutput ('ls-l') print (retcode) 0 print (output) Total consumption 160 drwxr-xr-x 2 wader wader 4096 December 7 2015 Public drwxr-xr-x 2 wader wader 4096 December 7 2015 template drwxr-xr-x 2 wader wader 4096 December 7 2015 Video drwxr-xr-x 2 wader wader 4096 December 7 2015 Picture drwxr-xr-x 2 wader wader 4096 December 7 2015 document drwxr-xr-x 2 wader wader 4096 April 13 download drwxr-xr -x 2 wader wader 4096 December 7 2015 Music drwxr-xr-x 7 wader wader 4096 May 26 2016 Desktop retcode Output = subprocess.getstatusoutput ('ls-l / test') print (retcode) 2 print (output) ls: unable to access / test: there is no copy code for that file or directory

Subprocess.Popen describes this class for executing a subroutine in a new process. As we mentioned earlier, the functions described above are implemented based on the subprocess.Popen class, and some common requirements can be accomplished in many ways by using these encapsulated high-level functions. Because the process creation and management at the bottom of the subprocess module is handled by the Popen class, we can do it through the flexible api provided by the subprocess.Popen class when we are unable to implement some less common functions through the above high-level functions.

1.subprocess.Popen 's constructor class subprocess.Popen (args, bufsize=-1, executable=None, stdin=None, stdout=None, stderr=None, preexec_fn=None, close_fds=True, shell=False, cwd=None, env=None, universal_newlines=False, startup_info=None, creationflags=0, restore_signals=True, start_new_session=False, pass_fds= ())

Parameter description:

Args: the shell command to be executed can be a string or a sequence of parameters of the command. When the value of the parameter is a string, the interpretation of the command is platform-specific, so it is generally recommended that the args parameter be passed as a sequence. Bufsize: specifies the caching policy. 0 indicates no buffering, 1 indicates row buffering, other numbers greater than 1 indicate buffer size, and negative numbers indicate the use of the system default buffering policy. Stdin, stdout, stderr: represent standard input, output and error handle of the program, respectively. Preexec_fn: used to specify an executable object that will be called before the child process runs, valid only on the Unix platform. Close_fds: if the value of this parameter is True, all file descriptors except 0J1 and 2 will be turned off before the child process executes. Shell: this parameter is used to identify whether to use shell as the program to be executed. If the args value is True, it is recommended that you pass the shell parameter as a string rather than as a sequence. Cwd: if the parameter value is not None, the function will change the current working directory before executing the child process. Env: the environment variable used to specify the child process. If env=None, then the environment variable of the child process will be inherited from the parent process. If envailing none, its value must be a mapping object. Universal_newlines: if the parameter value is True, the stdin,stdout and stderr of the file object will be opened as text streams, otherwise they will be opened as binary streams. Startupinfo and creationflags: these two parameters are valid only under Windows and will be passed to the underlying CreateProcess () function to set some properties of the child process, such as the appearance of the main window, process priority, and so on. 2. The method description Popen.poll () that can be called by an instance of the subprocess.Popen class is used to check whether the child process (command) has finished execution, returns None if not finished, and returns a status code when it is finished. Popen.wait (timeout=None) waits for the child process to finish and returns the status code; if the process is not finished after the number of seconds specified by timeout, a TimeoutExpired exception will be thrown. Popen.communicate (input=None, timeout=None) this method can be used to interact with processes, such as sending data to stdin and reading data from stdout and stderr until the end of the file is reached. Popen.send_signal (signal) sends the specified signal to the child process. Popen.terminate () stops the child process. Popen.kill () kills the child process. Note about the communicate () method: the optional parameter input in this method should be the data to be sent to the child process, or if no data is sent to the child process, it should be None. The data type of the input parameter must be a byte string, and if the universal_newlines parameter value is True, the data type of the input parameter must be a string. This method returns a tuple (stdout_data, stderr_data) that will be a byte punch or string (if the value of universal_newlines is True). If the process has not finished after the number of seconds specified by timeout, a TimeoutExpired exception will be thrown. Catch this exception and then try to communicate again without losing any output data. However, the child process is not killed after the timeout, in order to reasonably clear the corresponding content, a good application should manually kill the child process to end the communication. It is important to note that the data read here is buffered in memory, so this method should not be used if the data size is very large or infinite. 3. Subprocess.Popen uses example 1: copy the code

Import subprocess

P = subprocess.Popen ('df-Th', stdout=subprocess.PIPE Shell=True) print (p.stdout.read ()) Filesystem Type Size Used Avail Use% Mounted on / dev/vda1 ext4 40G 12G 26G 31% / devtmpfs devtmpfs 3.9G 0 3.9G 0 / devtmpfs tmpfs 3.9G 0 3.9G 0 / dev/shm tmpfs tmpfs 3.9G 386M 3.5G 10% / run tmpfs tmpfs 3.9G 03.9G 0 0% / sys/fs/cgroup tmpfs tmpfs 783M 0783M 0% / run/user/0 tmpfs tmpfs 783M 0783M 0% / run/ User/1000 copy code example 2: copy code 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') out,err = obj.communicate () print (out) 1 2 3

Print (err) copy code example 3: copy code obj = subprocess.Popen (["python"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) out,err = obj.communicate (input='print (1)\ n') print (out) 1

Print (err) copy code example 4: implement a function similar to the df-Th | grep data command, which actually implements the common function of pipes in shell.

Copy the code

P1 = subprocess.Popen (['df','-Th'], stdout=subprocess.PIPE) p2 = subprocess.Popen ([grep', 'data'], stdin=p1.stdout, stdout=subprocess.PIPE) out,err = p2.communicate () print (out) / dev/vdb1 ext4 493G 4.8G 463G 2% / data / dev/vdd1 ext4 1008G 420G 537G 44% / data1 / dev/vde1 ext4 985G 503G 432G 54% / data2

Print (err) None copy code

Fourth, summary so which module and which function should we use to execute commands to interact with the system and the system? Let's make a summary:

The first thing you should know is that the Python2.4 version introduces the subprocess module to replace functions such as os.system (), os.popen (), os.spawn* (), and commands module; that is, if you are using Python2.4 or above, you should use the subprocess module. If your application uses Python 2.4 or above, but Python 3.5 or less, Python's official recommendation is to use the subprocess.call () function. A new subprocess.check_call () function has been added in Python 2.5. a new subprocess.check_output () function has been added in Python 2.7. these two functions can also be used on demand. If your app uses Python 3.5 or above (which should be rare at the moment), Python's official advice is to use the subprocess.run () function as much as possible. When the high-level functions subprocess.call (), subprocess.check_call (), subprocess.check_output (), and subprocess.run () cannot meet the requirements, we can use the subprocess.Popen class to implement the complex functions we need.

The above is how to use the subprocess module in Python. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report