popen python_Python子进程和Popen()及其示例
popen python
Python provides a lot of functionalities with different modules and libraries. When we need to open a new process inside a Python application we can use subprocess library and related functions.
Python提供了许多具有不同模块和库的功能。 当我们需要在Python应用程序中打开新进程时,可以使用subprocess进程库和相关函数。
导入子流程模块 (Import subprocess Module)
In order to use related functions and libraries, we should import the subprocess library.
为了使用相关的功能和库,我们应该导入subprocess库。
import subprocess
使用call()函数启动子流程 (Start Sub-Process with call() Function)
The simples use case to create a subprocess is using call() function. call() function accepts related binary or executable name and parameters as Python list. In this example, we will call Linux ls command with -l and -a parameters.
创建子流程的简单用例是使用call()函数。 call()函数接受相关的二进制或可执行文件名称和参数作为Python列表。 在此示例中,我们将使用-l和-a参数调用Linux ls命令。
subprocess.call(['ls','-l','-a'])
创建自己的外壳程序(Create Own Shell For Process)
While creating a new process the newly created process will use current python shell and associated variables. We can specify the to create a new shell with the shell=True like below.
在创建新进程时,新创建的进程将使用当前的python shell和关联的变量。 我们可以指定来使用shell=True创建一个新的shell,如下所示。
subprocess.call(['ls','-l','-a'],shell=True)
保存过程输出(标准输出)(Save Process Output (stdout))
After running a given command or binary some output may be created. By default, this output is printed to the stdout which is a Python shell for our examples. We can save the process output to a Python variable by calling check_output command like below. In this example, we will save.
运行给定命令或二进制文件后,可能会创建一些输出。 默认情况下,此输出被打印到标准输出,该输出是我们示例的Python shell。 我们可以通过调用如下的check_output命令将过程输出保存到Python变量中。 在此示例中,我们将保存。
output=subprocess.check_output(['ls','-l','-a'])
We can see that all created output is saved into the variable named output. When we try to print this variable content to the terminal we will see the exact run command output which is the file list with a user, owner, and access time information.
我们可以看到所有创建的输出都保存到名为output的变量中。 当我们尝试将变量内容打印到终端时,我们将看到确切的运行命令输出,即包含用户,所有者和访问时间信息的文件列表。
将输出转换为字符串而不是字节数组 (Convert Output To A String Instead of Byte Array)
In the previous example, we have seen that the output is not a formatted state because the output is saved as a byte array into the output variable. We will provide the output as a string by using universal_newlines option as true for check_output() function.
在前面的示例中,我们已经看到输出不是格式化状态,因为输出已作为字节数组保存到output变量中。 我们将对check_output()函数使用universal_newlines选项设置为true,以字符串形式提供输出。
output=subprocess.check_output(['ls','-l','-a'],universal_newlines=True)
从流程中获取数据(Get Data From Process)
We can communicate with the process by opening the process with Popen() function. We will set the created process instance into a variable and then use communicate() function which will read the process pipe. In this example, we will put hello poftut with the echo command into a pipe and then read with communicate()
我们可以通过使用Popen()函数打开流程来与流程进行通信。 我们将创建的流程实例设置为变量,然后使用communicate()函数读取流程管道。 在此示例中,我们将把带有echo命令的hello poftut放入管道中,然后使用communicate()进行读取
p = subprocess.Popen(["echo", "hello poftut"], stdout=subprocess.PIPE)
print(p.communicate())
如果启动的进程有错误,则引发异常(Throw Exception If Started Process Has An Error)
While running commands with the call() function we may get some error related with the provided command. The error will be printed to the screen like a regular output for the Python.
使用call()函数运行命令时,我们可能会遇到一些与提供的命令有关的错误。 错误会像Python的常规输出一样显示在屏幕上。
This may be misinterpreted like the command is completed successfully and without error, so the output is printed to the screen. If we want to create some watchdog about command errors we can use check option which will raise an exception if the command has errors.
就像命令成功完成并且没有错误一样,这可能会被误解,因此输出会输出到屏幕上。 如果要创建有关命令错误的监视程序,可以使用check选项,如果命令有错误,它将引发异常。
subprocess.call(["ls","foo bar"],check=True)
翻译自: https://www.poftut.com/python-subprocess-popen-examples/
popen python