python

python中执行linux命令

2020-07-01  本文已影响0人  迷糊银儿
# encoding: utf-8 #有中文需加这个
import os,logging
logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger=logging.getLogger(__name__)

def exec_cmd(cmd):
        if  cmd.startswith('w'):
                print '%s not valid' % cmd
        else:
                ret=os.system(cmd)
                if ret!=0:
                        print "cmd \"%s\"执行失败" % cmd
                else:
                        logger.info("FATAL: exec cmd \"%s\" return %d.", cmd, ret)
if __name__=="__main__":
        cmd="pwd"
        exec_cmd(cmd)
上一篇下一篇

猜你喜欢

热点阅读