.os.system方式
#!/usr/bin/env Python
#coding:utf-8
import os
#执行成功则得到返回值0
ret=os.system('cat /TOOLS/python/test.txt')
print ret
#执行成功则得到返回值大于0
ret=os.system('cat /TOOLS/python/test1.txt')
print ret
[root@ansible python]# python ossystem.py
1111
0
cat: /TOOLS/python/test1.txt: No such file or directory
256
.os.popen方式
#!/usr/bin/env Python
#coding:utf-8
import os
#执行成功则得到命令输出
output=os.popen('cat /TOOLS/python/test.txt')
print output.readlines()
[root@ansible python]# python popen.py
['1111\n', '1111\n', '1111\n', '1111\n']
.commands方式
#!/usr/bin/env Python
#coding:utf-8
import commands
#执行成功则得到命令输出
(status, output) = commands.getstatusoutput('cat /TOOLS/python/test.txt')
print status
print output
[root@ansible python]# python command.py
0
1111
1111
1111
1111