![popen python](https://i-blog.csdnimg.cn/blog_migrate/856d423cf4dad6d34d2433707f74ed6a.png)
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'])
![Start with call Function](https://i-blog.csdnimg.cn/blog_migrate/a8e19bab961d97bd5c6f18ef2f45aa5d.png)
创建自己的外壳程序(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)
![Create Own Shell For Process](https://i-blog.csdnimg.cn/blog_migrate/5f4df8c7189c97a081194bdade580066.png)