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 open external programs in Python

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Python how to open external programs, I believe that many inexperienced people do not know what to do, so this article summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

In writing code, you often encounter the need to open external programs in Python programs, so how to open external programs in Python? Today we will introduce four different ways for your reference collection.

Use os.system ()

Os.system (command) is the easiest way to import os the module and call its system () method.

Refer to the following examples:

I can open the qq program on my computer through two simple lines of code, because I am in the MacOS system, so I need to add an open before the executable file. If it is windows, I only need to fill in the absolute path of the executable file.

Now we can invoke a slightly more complex command to invoke the ping command in Python:

However, the disadvantage of os.system () is that it cannot get the returned result after the call, and it can only be obtained by:

Os.system ("ping www.baidu.com > result.txt") outputs the result pipeline to a local file.

The return value of system will only have 0 (success), and 1 and 2 will indicate failure.

To sum up, system () does three things:

Fork a child process from the main process.

Call the exec function of python in the child process to execute the command.

Call wait (blocking) in the main process to wait for the child process to finish.

If fork fails, the system () function returns-1.

Use os.popen ()

Os.popen (command) is called in a similar way to os.system (), except that it forks the child process to implement the caller by creating a pipeline.

We can get the execution result by reading the return object of popen.

In the above code, we can find that the returned result can be obtained by executing the read () method on the returned object.

Use subprocess.open ()

The subprocess module is used to generate child processes in Python. It can connect the standard input and output of the child process and get the return value of the child process.

The following is the parameter definition for the subprocess.Popen () module:

The parameter interpretation of subprocess.Popen ():

The following executes the tree command through subprocess.Popen (), and the tree structure displays all files and directories under the current program path:

The argument to Popen can be either a string or a list.

Refer to the following two examples:

Subprocess.Popen (["cat", "test.md"]) subprocess.Popen ("cat test.txt", shell=True)

There are more advanced uses of Popen (), please refer to the official documentation to learn.

Use subprocess.call ()

The subporcess module also has a call () method that can be used to call external commands more easily.

Subprocess.call (* popenargs, * * kwargs)

The method of using call is basically the same as that of Popen, and there is not much difference. Here is just one more way to use it.

Os.system () is used to simply execute the command and display the execution result.

Os.popen () is used to simply execute the command, cannot display the execution result, and can return the execution result through the variable.

Subprocess.Popen () is used to execute complex commands, display the execution results, and set the output.

Subprocess.call () is used to execute complex commands, display the execution results, and set the output.

After reading the above, have you mastered how to open external programs in Python? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

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

Development

Wechat

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

12
Report