python中执行linux命令方法3

[root@bogon IP]# vim bash3.py

!/usr/bin/env python

import subprocess
p = subprocess.Popen('ls', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
for line in p.stdout.readlines():
print line,
retval = p.wait()

测试:
[root@bogon IP]# chmod u+x bash3.py
[root@bogon IP]# python bash3.py
bash2.py
bash3.py
bash.py
ip.py
[root@bogon IP]#

你可能感兴趣的:(python中执行linux命令方法3)