加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – logger找不到文件

发布时间:2020-12-20 11:44:29 所属栏目:Python 来源:网络整理
导读:我输入此输入命令时收到此错误: $python3.4 cron_e2e.py -f test_web_events -E ctg-clickstream testbrad 错误: $python3.4 cron_e2e.py -f test_web_events -E ctg-clickstream testbradTraceback (most recent call last): File "cron_e2e.py",line 421
我输入此输入命令时收到此错误:

$python3.4 cron_e2e.py -f test_web_events -E ctg-clickstream testbrad

错误:

$python3.4 cron_e2e.py -f test_web_events -E ctg-clickstream testbrad
Traceback (most recent call last):
  File "cron_e2e.py",line 421,in <module>
    sys.exit(main(sys.argv))
  File "cron_e2e.py",line 368,in main
    log.addHandler(logging.FileHandler(logfile))
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/logging/__init__.py",line 1006,in __init__
    StreamHandler.__init__(self,self._open())
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/logging/__init__.py",line 1030,in _open
    return open(self.baseFilename,self.mode,encoding=self.encoding)
FileNotFoundError: [Errno 2] No such file or directory: '/Users/bli1/Development/QE/chun-qe-trinity-functional/qe/tests/qe/logs/testbrad_8_21_2015_cron.log'

错误发生在这一行:

log.addHandler(logging.FileHandler(logfile))

不太确定我的代码在哪里不为我创建日志文件

码:

def main(argv):
    "Test"
    exit_code = 0
    global me; me = os.path.basename(argv[0]) # name of this program
    global mydir; mydir = os.path.dirname(os.path.abspath(__file__))
    parser = argparse.ArgumentParser(description=main.__doc__)
    parser.add_argument("-f","--functional_test",metavar="FUNCTEST",dest="functest",help="Type of functional test")
    parser.add_argument("-p","--phase",action="append",metavar="POST|HDFS|HIVE|ALL",dest="phase",help="phase of test to run")
    parser.add_argument("-t","--testfile",metavar="TESTFILE",dest="testfile",default=os.path.join(mydir,"resources/cron.json"),help="file with test commands")
    parser.add_argument("-E","--Event",metavar="EVENTNAME",dest="event_names",help="[repeatable] topic to which to post")
    parser.add_argument("runtag",metavar="RUNTAG",help="run tag for this run")
    args = parser.parse_args(args=argv[1:])  # will exit on parse error
    log = logging.getLogger(me)
    log.setLevel(logging.INFO)
    dt = datetime.datetime.utcnow()
    logfile = "qe/logs/" + (args.runtag + "_{}_{}_{}".format(dt.month,dt.day,dt.year) + "_cron.log")

    pdb.set_trace()
    if isinstance(logfile,str):
        if os.path.exists(logfile):
            os.remove(logfile)
        # logging.FileHandler() returns FileHandler class,the file is opened and used as the stream for logging
        log.addHandler(logging.FileHandler(logfile))
    console = logging.StreamHandler(sys.stderr); console.setLevel(logging.WARNING); log.addHandler(console)

解决方法

问题是您定义要转到的日志文件的目录 – “qe / logs /” – 不存在.

默认情况下,FileHandler的模式为“a”,这意味着如果日志文件存在,它将打开它并在最后开始追加它,否则它将创建该文件.

但是FileHandler只会创建日志文件,如果它不存在,它就不会创建目录,如果该目录不存在,它会抛出一个类似你的错误.

您需要手动或以编程方式自己创建目录.如果要以编程方式创建目录,可以使用 – os.makedirs() – 这将 –

create all intermediate-level directories needed to contain the leaf directory.

示例 –

import os,os.path
if not os.path.exists("qe/logs/"):
    os.makedirs("qe/logs/")

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读