In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the knowledge of "how python runs the cmd command". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
1. Use the os.system () method
Os.system is used to execute cmd instructions. The output in cmd will be output directly in the console. A result of 0 indicates that the execution is successful.
Sample code:
# coding:utf-8import osos.system ("ls")
The PS:os.system () method simply executes the cmd instruction rudely, and there is no way to get the contents of the cmd output.
After calling the shell script, the PPS:os.system () method returns a 16-bit binary number, the low bit is the signal number that kills the called script, and the high bit is the exit status code of the script, that is, after the "exit 1" code in the script is executed, the high bit number of the os.system function return value is 1. If the low bit number is 0, the return value of the function is 0x0100, which is converted to decimal system to get 256.
2. Use the os.popen () method
The os.popen () method is used to open a pipe from a command. Valid in Unix,Windows. What os.popen returns is a file object, just like open opens a file.
The syntax format of the popen () method is as follows:
Os.popen (command [, mode [, bufsize]])
Parameters:
Command-the command used. Mode-Mode permissions can be'r'(default) or'w'. Bufsize-indicates the size of the buffer required by the file: 0 means no buffer; 1 means line buffer; and other positive values indicate buffering using parameter size (approximate values, in bytes). A negative bufsize means using the default value of the system, which is generally line buffering for tty devices and full buffering for other files. If you do not change the parameters, use the default values of the system.
Sample code:
# coding:utf-8import os# popen returns the file object, like the open operation, f = os.popen (r "ls", "r") l = f.read () print (l) f.close ()
The return value is the recommended way to handle file objects:
With os.popen (cmd, "r") as p: r = p.read ()
To use it, you don't need to write p.close () explicitly.
The PS:os.popen () method is nonblocking.
3. Use subprocess.Popen ()
After the python2.4 version, the subprocess module is provided to generate the child process, and can connect to the child process's standard input, output, error, and get the return value of the child process.
Subprocess.Popen () is defined as follows:
Class subprocess.Popen (args, bufsize=0, executable=None, stdin=None, stdout=None, stderr=None, preexec_fn=None, close_fds=False, shell=False, cwd=None, env=None, universal_newlines=False, startupinfo=None, creationflags=0)
The significance of its parameters will not be repeated here. A simple sample code:
# coding:utf-8import subprocessp = subprocess.Popen ("ls", shell=True, stdout=subprocess.PIPE) r = p.stdout.read () print (r)
PS:subprocess provides two ways to call subroutines. Subprocess.call () is blocking and subprocess.Popen () is non-blocking.
That's all for "how python runs the cmd command". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.