python open找不到文件的原因_python – logger找不到文件

我输入此输入命令时收到此错误:

$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

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", action="append",

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/")

标签:python,python-3-x,logging

来源: https://codeday.me/bug/20190727/1557884.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
*** error 56: cannot open file 是一个错误信息,表示无法打开文件。根据提供的引用内容,这个错误可能是由于以下原因之一引起的: 1. 文件路径错误:可能是文件路径没有正确设置或者文件不存在。您可以检查文件路径是否正确,并确保文件存在。 2. 权限问题:可能是由于权限限制导致无法访问该文件。您可以尝试使用管理员权限或者检查文件的权限设置。 3. 编译器设置问题:可能是由于编译器设置不正确导致的。您可以检查编译器的设置,特别是头文件路径是否正确设置并添加到工程中。 综上所述,*** error 56: cannot open file 错误可能是由于文件路径错误、权限问题或者编译器设置问题引起的。您可以根据具体情况进行排查和调整。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Windows下MySQL 5.7无法启动的解决方法](https://download.csdn.net/download/weixin_38722944/14838296)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [keil5 之error 56: cannot open file](https://blog.csdn.net/RONG_YAO/article/details/112555986)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [问题:在keil仿真时出现error 56: can‘t open file原因总结](https://blog.csdn.net/qq_48701278/article/details/115640961)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值