最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】PyCharm中创建logger的RotatingFileHandler出错:FileNotFoundError Errno 2 No such file or directory

PyCharm crifan 8045浏览 0评论

折腾:

【已解决】Flask中添加log且输出到文件中

期间,对于之前可以正常运行的Flask的logger的代码:

<code>fileHandler = RotatingFileHandler(
    app.config['LOG_FILE_FILENAME'],
    maxBytes=2*1024*1024,
    backupCount=3,
    encoding="UTF-8")
fileHandler.setLevel(logging.DEBUG)
</code>

此处竟然出错:

<code>/Users/crifan/.virtualenvs/robotDemo-HXjMJQEQ/bin/python /Applications/PyCharm.app/Contents/helpers/pydev/pydevd.py --multiproc --qt-support=auto --client 127.0.0.1 --port 55120 --file /Users/crifan/dev/dev_root/company/naturling/projects/robotDemo/testRestApi.py
pydev debugger: process 7944 is connecting

Connected to pydev debugger (build 173.4127.16)
Traceback (most recent call last):
  File "/Applications/PyCharm.app/Contents/helpers/pydev/pydevd.py", line 1668, in &lt;module&gt;
    main()
  File "/Applications/PyCharm.app/Contents/helpers/pydev/pydevd.py", line 1662, in main
    globals = debugger.run(setup['file'], None, None, is_module)
  File "/Applications/PyCharm.app/Contents/helpers/pydev/pydevd.py", line 1072, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "/Applications/PyCharm.app/Contents/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "/Users/crifan/dev/dev_root/company/naturling/projects/robotDemo/testRestApi.py", line 28, in &lt;module&gt;
    encoding="UTF-8")
  File "/usr/local/Cellar/python/3.6.4_4/Frameworks/Python.framework/Versions/3.6/lib/python3.6/logging/handlers.py", line 150, in __init__
    BaseRotatingHandler.__init__(self, filename, mode, encoding, delay)
  File "/usr/local/Cellar/python/3.6.4_4/Frameworks/Python.framework/Versions/3.6/lib/python3.6/logging/handlers.py", line 57, in __init__
    logging.FileHandler.__init__(self, filename, mode, encoding, delay)
  File "/usr/local/Cellar/python/3.6.4_4/Frameworks/Python.framework/Versions/3.6/lib/python3.6/logging/__init__.py", line 1030, in __init__
    StreamHandler.__init__(self, self._open())
  File "/usr/local/Cellar/python/3.6.4_4/Frameworks/Python.framework/Versions/3.6/lib/python3.6/logging/__init__.py", line 1059, in _open
    return open(self.baseFilename, self.mode, encoding=self.encoding)
FileNotFoundError: [Errno 2] No such file or directory: '/Applications/PyCharm.app/Contents/bin/logs/RobotQA.log'

</code>

先去创建了对应的log文件:

但是还是不行。

注意到此处错误中路径是:

/Applications/PyCharm.app/Contents/bin

而不是此处项目的根目录

所以觉得很是奇怪。

最后无意间发现:

原来自动用Control +D去生成的debug的配置中,竟然没有设置此处的working directory:

所以去加上:

然后再去试试,就可以了:

【总结】

此处由于之前:

【已解决】PyCharm一直在重新indexing建立索引

而清空了项目缓存,新建的项目,新建的index索引,

用Control+D去自动让PyCharm新建的debug调试的配置

-》之前正常情况下:会自动帮忙设置好 working directory的

-〉此处异常情况:没有自动帮忙设置working directory

-》导致后续执行代码,对于文件路径:

logs/RobotQA.log

前缀路径用的是,PyCharm默认的:

/Applications/PyCharm.app/Contents/bin

所以才找不到此处的项目根目录:

/Users/crifan/dev/dev_root/company/naturling/projects/robotDemo

下的log文件

其完整路径应该是:

/Users/crifan/dev/dev_root/company/naturling/projects/robotDemo/logs/RobotQA.log

解决办法:

修改默认的debug配置,把working directory设置为当前项目根目录:

即可。

转载请注明:在路上 » 【已解决】PyCharm中创建logger的RotatingFileHandler出错:FileNotFoundError Errno 2 No such file or directory

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
89 queries in 0.168 seconds, using 22.12MB memory