Python执行命令行获取输出

#!/usr/bin/env python
#
# $id: refresh.py,v 1.0.0 2009/01/05 15:32:48 master Exp $
# Copyright (C) 2009 Yunlong Wen ([email protected])
#
# Last Modified: 2009/01/05 16:19:12
 
import fileinput
import os
from os import curdir, sep
 
class Shell:
 
    def exec(self, cmdline):
        out = os.popen( cmdline, 'r')
        txt = out.read()
        ret = out.close()
        if ret:
            raise RuntimeError, '%s failed with exit code %d' % (cmdline, ret)
	    return '%s failed with exit code %d' % (cmdline, ret)
        else:
            return  txt

Leave a comment

Your comment