In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/03 Report--
Taking Python as an example, this paper analyzes the methods of calling system commands by Python and how these ways are realized. Read the complete article, I believe that you have a certain understanding of the method of calling system commands by Python.
Os or subprocess module is generally used to call system commands in Python. Here are the six most commonly used methods of calling system commands in Python.
1. Os.system ()
This function returns the return value of the result of the execution of the command, and the system () function performs 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. Call wait (blocking) in the parent process to wait for the child process to finish.
Returning 0 indicates successful execution of the command, while others indicates failure.
Usage:
Os.system ("command")
2. Os.popen ()
This call method is achieved through the pipeline. The function returns the object of file read, and the output of the execution can be seen by reading read, readlines and other operations.
Usage:
Os.popen ("command")
3. Subprocess.Popen ()
Subprocess module is recommended to replace some old modules and functions, such as os.system, os.spawn, os.popen, etc.
The purpose of the subprocess module is to fork and communicate with a new process. The most common is to define the class Popen. Using Popen, you can create a process and interact with it in a complex way.
Usage:
Child = subprocess.Popen (["cmd", "arg1"...])
4. Subprocess.call ()
Executes the specified command and returns the command execution status, which is similar to os.system (cmd).
Usage:
Subprocess.call ("command")
5. Subprocess.run ()
The new function in Python 3.5 executes the specified command and waits for the command execution to return an instance of the CompletedProcess class that contains the execution result.
Usage:
Subprocess.run (args, *, stdin=None, input=None, stdout=None, stderr=None, shell=False, timeout=None, check=False, universal_newlines=False)
6. Subprocess.getstatusoutput ()
Executes the cmd command and returns a tuple (command execution status, command execution result output), which functions similar to commands.getstatusoutput ().
Usage:
Subprocess.getstatusoutput ("command")
These are the six ways for Python to call system commands, and you need to use them yourself in order to understand the details. If you want to know more about it, 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.