Detailed explanation of how to execute system commands in Python

巴扎黑
Release: 2017-09-16 10:07:09
Original
1576 people have browsed it

Recently when I was working on that testing framework, I found that I was not familiar with executing system commands in Python, so I wanted to summarize. The following article mainly introduces you to the method of executing system commands in Python. Friends who need it can For reference, let’s take a look below.

Preface

Python is often called the "glue language" because it can easily operate other programs and easily package other languages. Library written. In the Python/wxPython environment, a method of executing external commands or starting another program in a Python program.

This article will introduce in detail the relevant information on how to execute system commands in Python. I won’t say much below, let’s take a look at the detailed introduction.

(1) os.system()

This method directly calls standard C’s system()Function only runs system commands in a sub-terminal and cannot obtain the information returned by execution.


>>> import os 
>>> output = os.system('cat /proc/cpuinfo') 
processor : 0 
vendor_id : AuthenticAMD 
cpu family : 21 
... ... 
>>> output # doesn't capture output 
0
Copy after login

(2) os.popen()

This method Executing the command and returning the executed information object returns the result through a pipeline file.


>>> output = os.popen('cat /proc/cpuinfo') 
>>> output 
<open file &#39;cat /proc/cpuinfo&#39;, mode &#39;r&#39; at 0x7ff52d831540> 
>>> print output.read() 
processor : 0 
vendor_id : AuthenticAMD 
cpu family : 21 
... ... 
>>><span style="font-size:14px;">
Copy after login

(3) commands module


>>> import commands 
>>> (status, output) = commands.getstatusoutput(&#39;cat /proc/cpuinfo&#39;) 
>>> print output 
processor : 0 
vendor_id : AuthenticAMD 
cpu family : 21 
... ... 
>>> print status 
0
Copy after login

Note 1: The return value (status) returned by this method under a Unix-like system is not the same as the return value after the script or command is executed. This is Because os.wait() is called, you have to understand the implementation of system wait() for the specific reason. If you need the correct return value (status), you only need to right-shift the return value by 8 bits.

Note 2: When the parameters or returns of the execution command contain Chinese characters, it is recommended to use subprocess.

(4) subprocess module

#This module is a powerful subprocess management module and is a replacement# A module for ##os.system, os.spawn* and other methods.


>>> import subprocess 
>>> subprocess.Popen(["ls", "-l"]) <strong> # python2.x</strong> doesn&#39;t capture output 
>>> subprocess.run(["ls", "-l"])  <strong># python3.x</strong> doesn&#39;t capture output 
<subprocess.Popen object at 0x7ff52d7ee490> 
>>> total 68 
drwxrwxr-x 3 xl xl 4096 Feb 8 05:00 com 
drwxr-xr-x 2 xl xl 4096 Jan 21 02:58 Desktop 
drwxr-xr-x 2 xl xl 4096 Jan 21 02:58 Documents 
drwxr-xr-x 2 xl xl 4096 Jan 21 07:44 Downloads 
... ... 
>>>
Copy after login

The above is the detailed content of Detailed explanation of how to execute system commands in Python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!