Python执行Shell命令测试脚本

logging.conf

[loggers]
keys=root

[handlers]
keys=rotateFileHandler

[formatters]
keys=logformat

[formatter_logformat]
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s

[logger_root]
level=DEBUG
handlers=rotateFileHandler

[handler_rotateFileHandler]
class=handlers.RotatingFileHandler
level=NOTSET
formatter=logformat
args=('ExecuteOraSql.log', 'a', 10*1024*1024, 10)

ExecuteOraSql.py

import subprocess
import logging
import logging.config

if __name__ == '__main__':

    log_filename = "logging.conf"
    logging.config.fileConfig(log_filename)
    logger = logging.getLogger("ExecuteOraSql")

    cmd = "sqlplus -S user/pwd @"
    for x in 1,2:
        sql = str(x) + ".sql"
        p = subprocess.Popen(cmd+sql, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        o = p.stdout.readlines()
        e = p.stderr.readlines()
        for i in o:
                logger.debug(i)
        if e:
                logger.error(e)

参考:

http://www.cnblogs.com/dkblog/archive/2011/08/26/2155018.html

http://blog.csdn.net/dyx1024/article/details/7250828

你可能感兴趣的:(Python)