zhouer 发表于 2018-8-8 13:36:50

Python 执行Shell 外部命令

  1、os.system()
  此方法执行的外部程序,会将结果直接输出到标准输出。os.system的返回结果为执行shell 的 $? 值。
  因此请执行没有输出结果的程序时适合使用此方法。如touch 、rm 一个文件等。
In : import os  
In : os.system('touch test.txt')
  
Out: 0
  
In : os.system('rm -rf test.txt')
  
Out: 0
  2、os.popen()
  此方法结合了os.system和 文件的特性。可以解决os.system那种无法获取程序执行结果的缺点
  os.popen 返回了一个类似与文件句柄的东西。可以对这个返回的结果做一些类似于对文件的操作
In : output = os.popen('cat /etc/passwd')  

  
In : for line in output.readlines():
  
   ...:   print line.strip()
  
   ...:
  
root:x:0:0:root:/root:/bin/bash
  
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
  
bin:x:2:2:bin:/bin:/usr/sbin/nologin
  
sys:x:3:3:sys:/dev:/usr/sbin/nologin
  
sync:x:4:65534:sync:/bin:/bin/sync
  
games:x:5:60:games:/usr/games:/usr/sbin/nologin
  可以help(output) 获取相关帮助去操作这个output
  3、commands 模块
  这个模块主要提供了三个方法:
  (1)、getoutput(cmd)
  Return output (stdout or stderr) of executing cmd in a shell.
  返回一个shell 命令的标准输出或者时错误输出
In : commands.getoutput('ls /home -l')  
Out: 'total 4\ndrwxr-xr-x 31 admin admin 40965\xe6\x9c\x883 09:48 admin'
  

  
In : commands.getoutput('ls /homeaaa -l')
  
Out: 'ls: cannot access /homeaaa: No such file or directory'
  (2)、getstatus(file)
  Return output of "ls -ld <file>" in a string.
  获取一个文件的状态。 相当于执行了 ls -ld file
In : commands.getstatus('/bin/ls')  
Out: '-rwxr-xr-x 1 root root 1100803\xe6\x9c\x88 242014 /bin/ls' // 相当于执行了 ls -ld /bin/ls
  

  
In : os.system('ls -ld /bin/ls')
  
-rwxr-xr-x 1 root root 1100803月 242014 /bin/ls
  (3)、getstatusoutput(cmd)
  Return (status, output) of executing cmd in a shell.
  返回shell 的状态码和输出结果
In : commands.getstatusoutput('ls /home -l')  
Out: (0, 'total 4\ndrwxr-xr-x 31 admin admin 40965\xe6\x9c\x883 09:48 admin')
  

  
In : commands.getstatusoutput('ls /homeaa -l')
  
Out: (512, 'ls: cannot access /homeaa: No such file or directory')
  4、subprocess 相关模块.
  从这个模块的帮助中可以看到。它的一个很主要的目的就是要替代掉os.system 、os.spawn*、os.popen*、popen2.* 、commands.*这些模块的功能
  subproces 模块中一个很强大的类Popen,我们的主要学习重点应该在于此

  >  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):
  args: 可以是一个字符串 或者是一个序列(list | tuple )。其实字符串也是序列呀。呵呵,我们这里就讲序列认为时list 和 tuple 了吧。若是序列时,则第一个元素为可执行的命令
  在unix 系统中, shell=Ture 和 shell=False(默认) 和 args 参数有一定的影响关系。
  看一下下面的列子:
  subprocess.Popen(["cat","/etc/passwd"])
  subprocess.Popen("cat /etc/passwd")
  我们看看以上这两种写法是否OK
In : subprocess.Popen(["cat","/etc/passwd"])  
root:x:0:0:root:/root:/bin/bash
  
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
  
bin:x:2:2:bin:/bin:/usr/sbin/nologin
  
sys:x:3:3:sys:/dev:/usr/sbin/nologin
  
sync:x:4:65534:sync:/bin:/bin/sync
  

  
In : subprocess.Popen("cat /etc/passwd")
  
---------------------------------------------------------------------------
  
OSError                                 Traceback (most recent call last)
  第一种写法OK。第二种写法不OK。这是什么原因呢?
  主要时 shell=False(默认值) 这个参数搞的鬼。
  在UNIX中, shell=False 时 subprocess.Popen() 使用os.execvp()去执行响应的子程序。
  当 args 是一个字符串的时候,Popen 认为这个字符串是序列中的第一个元素(可执行的程序)。
  调用 os.execvp()的时候 ,到$PATH 中去找这个可执行程序,没有找到,所以程序出现了异常。
  不过这也分命令,若写成如下的形式:
  subprocess.Popen("/bin/pwd")
In : subprocess.Popen("/bin/pwd")  
/home/dexin/python/tcollector
  以上这个就无所谓了。
  不过就是想让 subprocess.Popen("cat /etc/passwd") 能够正确的去执行,应该怎么办呢? 设置 shell=True .这样当再执行类似的程序时,会调用相应的shell 去做shell -c "cat /etc/passwd"
subprocess.Popen("cat /etc/passwd",shell=True)  
Out: <subprocess.Popen at 0x7f922a6b2350>
  

  
In : root:x:0:0:root:/root:/bin/bash
  
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
  
bin:x:2:2:bin:/bin:/usr/sbin/nologin
  
sys:x:3:3:sys:/dev:/usr/sbin/nologin
  
sync:x:4:65534:sync:/bin:/bin/sync
  
games:x:5:60:games:/usr/games:/usr/sbin/nologin
  
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
  
lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
  
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
  
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
  
uucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologin
页: [1]
查看完整版本: Python 执行Shell 外部命令