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 execute cmd in Python

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

Share

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

This article mainly introduces how to implement cmd in Python, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

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) Thank you for reading this article carefully. I hope the article "how to implement cmd in Python" shared by the editor will be helpful to you. At the same time, I hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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