方法一:
[python] view plain copy
- import os
- p = os.popen('uptime')
- x=p.read()
- print x
方法二:
[python] view plain copy
- import subprocess
- res = subprocess.Popen('uptime',shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE,close_fds=True)
- result = res.stdout.readlines()
補充:
方法三:
[python] view plain copy
- import commands
- cmd = "tail -n 5 tmpfile"
- _, content = commands.getstatusoutput(cmd)
方法1.2轉自:http://blog.csdn.net/dingyaguang117/article/details/7236296