赞
踩
最近在引入python中的日志模块:logging来添加日志,但是发现在引入日志的存放路径时一直报错:
FileNotFoundError: [Errno 2] No such file or directory
我所有的的url地址封装在setting.py文件中,代码如下:
import os
#项目根目录
PROJECT_ROOT=os.path.dirname(os.path.abspath('__file__'))
print(PROJECT_ROOT)
#日志目录
LOG_PATH=os.path.join(PROJECT_ROOT,'log','api_test.log')
print(LOG_PATH)
打印结果如下:
/Users/接口测试代码/interface_back_test20.9.23
/Users/接口测试代码/interface_back_test20.9.23/log/api_test.log
如果这样来看的话,我的日志的存放路径是没有问题的,但是我在日志文件log.py文件中引入的时候,一直报错如下:
Traceback (most recent call last):
File “/Users/接口测试代码/interface_back_test20.9.23/lib/log.py”, line 24, in
init_logging()
File “/Users/接口测试代码/interface_back_test20.9.23/lib/log.py”, line 13, in init_logging
fh=logging.handlers.TimedRotatingFileHandler(filename=LOG_PATH, when=‘D’, interval=1, backupCount=7,
File “/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/logging/handlers.py”, line 200, in init
BaseRotatingHandler.init(self, filename, ‘a’, encoding, delay)
File “/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/logging/handlers.py”, line 55, in init
logging.FileHandler.init(self, filename, mode, encoding, delay)
File “/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/logging/init.py”, line 1143, in init
StreamHandler.init(self, self._open())
File “/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/logging/init.py”, line 1172, in _open
return open(self.baseFilename, self.mode, encoding=self.encoding)
FileNotFoundError: [Errno 2] No such file or directory: '/Users/接口测试代码/interface_back_test20.9.23/lib/log/api_test.log’
主要看最后一句报错,还是找不到相应的文件目录,然后我就定位到第13行,发现还是settng.py里面路径的配置写的有错误,后来经过调试终于发现了错误:
PROJECT_ROOT=os.path.dirname(os.path.abspath('__file__'))
file多加了引号,导致其他文件引入该路径的时候,直接报错!然后我去掉之后,再次引入,就没有这样的问题了,所以大家写的时候一定要注意相关的语法和规则!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。