os.system 和 os.popen 函数:
python 调用 shell 命令有 2 种方法: os.system() 和 os.popen()
os.system() 的返回值只会有 0(成功),1,2;os.popen() 会把执行命令的输出作为值返回, 可实现一个 "管道", 从这个命令获取的值可以继续被调用.
示例:
- vi a1.py
- #!/usr/bin/python
- import os
- a = os.popen('df -h /').readlines()
- a2 = ''.join(a)
- print a
- print a2
- b = os.system('df -h /')
- print b
- :wq
- python a1.py
来源: http://www.bubuko.com/infodetail-2631479.html