#!/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
#!/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']
#!/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