python learning --python execute shell commands

 

Here are four ways to look at python execute shell commands:

1, os the os.system module () function to execute shell commands

<span style="color:#333333"><span style="color:#333333"><span style="color:black"><code class="language-python"><span style="color:#9a6e3a">>></span><span style="color:#9a6e3a">></span> os<span style="color:#999999">.</span>system<span style="color:#999999">(</span><span style="color:#669900">'ls'</span><span style="color:#999999">)</span>
anaconda<span style="color:#9a6e3a">-</span>ks<span style="color:#999999">.</span>cfg  install<span style="color:#999999">.</span>log  install<span style="color:#999999">.</span>log<span style="color:#999999">.</span>syslog  send_sms_service<span style="color:#999999">.</span>py  sms<span style="color:#999999">.</span>py
<span style="color:#990055">0</span></code></span></span></span>

Note that this method can not get the output of shell commands.

Xiao Bian here recommend a Python learning exchange qq group: 882 492 178, there are a lot of content on the depth of learning, artificial intelligence, data processing.

If you have not read, never even contacted the programming, do not worry, we have a system where data prep, prep will take you to start from zero, adding hard to say goodbye to the entry of trouble.

2, popen () # This method can get the results after the command is a string, to deal with on their own in order to get the desired information.

<span style="color:#333333"><span style="color:#333333"><span style="color:black"><code class="language-python"><span style="color:#9a6e3a">>></span><span style="color:#9a6e3a">></span> <span style="color:#0077aa">import</span> os
<span style="color:#9a6e3a">>></span><span style="color:#9a6e3a">></span> <span style="color:#669900">str</span> <span style="color:#9a6e3a">=</span> os<span style="color:#999999">.</span>popen<span style="color:#999999">(</span><span style="color:#669900">"ls"</span><span style="color:#999999">)</span><span style="color:#999999">.</span>read<span style="color:#999999">(</span><span style="color:#999999">)</span>
<span style="color:#9a6e3a">>></span><span style="color:#9a6e3a">></span> a <span style="color:#9a6e3a">=</span> <span style="color:#669900">str</span><span style="color:#999999">.</span>split<span style="color:#999999">(</span><span style="color:#669900">"\n"</span><span style="color:#999999">)</span>
<span style="color:#9a6e3a">>></span><span style="color:#9a6e3a">></span> <span style="color:#0077aa">for</span> b <span style="color:#0077aa">in</span> a<span style="color:#999999">:</span>
        <span style="color:#0077aa">print</span> b</code></span></span></span>

Results obtained with this first method is the same.

3, commands module # can easily achieve an output command (including standard and error output) and the execution state bits

<span style="color:#333333"><span style="color:#333333"><span style="color:black"><code class="language-python"><span style="color:#0077aa">import</span> commands
a<span style="color:#999999">,</span>b <span style="color:#9a6e3a">=</span> commands<span style="color:#999999">.</span>getstatusoutput<span style="color:#999999">(</span><span style="color:#669900">'ls'</span><span style="color:#999999">)</span>
a是退出状态
b是输出的结果。
<span style="color:#9a6e3a">>></span><span style="color:#9a6e3a">></span> <span style="color:#0077aa">import</span> commands
<span style="color:#9a6e3a">>></span><span style="color:#9a6e3a">></span> a<span style="color:#999999">,</span>b <span style="color:#9a6e3a">=</span> commands<span style="color:#999999">.</span>getstatusoutput<span style="color:#999999">(</span><span style="color:#669900">'ls'</span><span style="color:#999999">)</span>
<span style="color:#9a6e3a">>></span><span style="color:#9a6e3a">></span> <span style="color:#0077aa">print</span> a
<span style="color:#990055">0</span>
<span style="color:#9a6e3a">>></span><span style="color:#9a6e3a">></span> <span style="color:#0077aa">print</span> b
anaconda<span style="color:#9a6e3a">-</span>ks<span style="color:#999999">.</span>cfg
install<span style="color:#999999">.</span>log
install<span style="color:#999999">.</span>log<span style="color:#999999">.</span>syslog</code></span></span></span>

commands.getstatusoutput(cmd)返回(status,output)

commands.getoutput (cmd) returns only the output

commands.getstatus (file) returns ls -ld file execution result string, called getoutput, do not recommend using this method.

 

4, subprocess module

 

Use subprocess module can create a new process, you can enter the new process / output / error pipes connectivity, and you can get the return status of the implementation of the new process. The purpose of the subprocess module (), os.popen * (), commands. * Old functions or the like substitute module os.system.

import subprocess

1、subprocess.call(command, shell=True)

# Will directly print out the results.

2, subprocess.Popen (command, shell = True) may be subprocess.Popen (command, stdout = subprocess.PIPE, shell = True) so that the results can be output.

If the command is not an executable file, shell = True can not be omitted.

shell = True means that the shell command execution

 

These four methods can execute shell commands.

 

Guess you like

Origin blog.csdn.net/qq_41753040/article/details/90322753