[英]Using RotatingFileHandler in python gives permission denied error python
Code:代码:
import sys
from flask import Flask
import logging
from logging.handlers import RotatingFileHandler
from datetime import datetime
app = Flask(__name__)
formatter = logging.Formatter('%(asctime)s.%(msecs)03d [%(thread)d] [%(levelname)s]: %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
file_hdlr = RotatingFileHandler('logs/debug_{}.log'.format(datetime.utcnow().strftime('%Y_%m_%d')), maxBytes= 1024, backupCount= 10)
file_hdlr.setFormatter(formatter)
file_logger = logging.getLogger('file_logger')
file_logger.setLevel(logging.INFO)
file_logger.addHandler(file_hdlr)
@app.route('/test', methods = ['POST'])
def test():
"""
This function is for testing API
Returns:
This function returns a string.
"""
for i in range(25):
file_logger.info(f"{i}")
return "API working correctly"
if __name__ == "__main__":
app.run(host = '0.0.0.0', port = 5001, debug = True, threaded = True)
Traceback:追溯:
--- Logging error ---
Traceback (most recent call last):
File "c:\users\aniket.bote\appdata\local\programs\python\python38\lib
self.doRollover()
File "c:\users\aniket.bote\appdata\local\programs\python\python38\libr
self.rotate(self.baseFilename, dfn)
File "c:\users\aniket.bote\appdata\local\programs\python\python38\lib
os.rename(source, dest)
PermissionError: [WinError 32] The process cannot access the file becauUsers\\aniket.bote\\Desktop\\Aniket\\DS_2021_Array_cable_optimization\\iket.bote\\Desktop\\Aniket\\DS_2021_Array_cable_optimization\\logs\\deb
Call stack:
File "c:\users\aniket.bote\appdata\local\programs\python\python38\lib
self._bootstrap_inner()
File "c:\users\aniket.bote\appdata\local\programs\python\python38\lib
self.run()
File "c:\users\aniket.bote\appdata\local\programs\python\python38\lib
self._target(*self._args, **self._kwargs)
File "c:\users\aniket.bote\appdata\local\programs\python\python38\libst_thread
self.finish_request(request, client_address)
File "c:\users\aniket.bote\appdata\local\programs\python\python38\libt
self.RequestHandlerClass(request, client_address, self)
File "c:\users\aniket.bote\appdata\local\programs\python\python38\lib
self.handle()
File "C:\Users\aniket.bote\Desktop\Aniket\DS_2021_Array_cable_optimiz.py", line 342, in handle
BaseHTTPRequestHandler.handle(self)
File "c:\users\aniket.bote\appdata\local\programs\python\python38\lib
self.handle_one_request()
File "C:\Users\aniket.bote\Desktop\Aniket\DS_2021_Array_cable_optimiz.py", line 374, in handle_one_request
self.run_wsgi()
File "C:\Users\aniket.bote\Desktop\Aniket\DS_2021_Array_cable_optimiz.py", line 319, in run_wsgi
execute(self.server.app)
File "C:\Users\aniket.bote\Desktop\Aniket\DS_2021_Array_cable_optimiz.py", line 310, in execute
for data in application_iter:
File "C:\Users\aniket.bote\Desktop\Aniket\DS_2021_Array_cable_optimiz_init__.py", line 309, in debug_application
app_iter = self.app(environ, start_response)
File "C:\Users\aniket.bote\Desktop\Aniket\DS_2021_Array_cable_optimizine 2088, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Users\aniket.bote\Desktop\Aniket\DS_2021_Array_cable_optimizine 2070, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\aniket.bote\Desktop\Aniket\DS_2021_Array_cable_optimizine 1513, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\aniket.bote\Desktop\Aniket\DS_2021_Array_cable_optimizine 1499, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**req.v
File "c:\Users\aniket.bote\Desktop\Aniket\DS_2021_Array_cable_optimiz
file_logger.info(f"{i}")
Message: '23'
Arguments: ()
--- Logging error ---
Traceback (most recent call last):
File "c:\users\aniket.bote\appdata\local\programs\python\python38\lib
self.doRollover()
File "c:\users\aniket.bote\appdata\local\programs\python\python38\libr
self.rotate(self.baseFilename, dfn)
File "c:\users\aniket.bote\appdata\local\programs\python\python38\lib
os.rename(source, dest)
PermissionError: [WinError 32] The process cannot access the file becauUsers\\aniket.bote\\Desktop\\Aniket\\DS_2021_Array_cable_optimization\\iket.bote\\Desktop\\Aniket\\DS_2021_Array_cable_optimization\\logs\\deb
Call stack:
File "c:\users\aniket.bote\appdata\local\programs\python\python38\lib
self._bootstrap_inner()
File "c:\users\aniket.bote\appdata\local\programs\python\python38\lib
self.run()
File "c:\users\aniket.bote\appdata\local\programs\python\python38\lib
self._target(*self._args, **self._kwargs)
File "c:\users\aniket.bote\appdata\local\programs\python\python38\libst_thread
self.finish_request(request, client_address)
File "c:\users\aniket.bote\appdata\local\programs\python\python38\libt
self.RequestHandlerClass(request, client_address, self)
File "c:\users\aniket.bote\appdata\local\programs\python\python38\lib
self.handle()
File "C:\Users\aniket.bote\Desktop\Aniket\DS_2021_Array_cable_optimiz.py", line 342, in handle
BaseHTTPRequestHandler.handle(self)
File "c:\users\aniket.bote\appdata\local\programs\python\python38\lib
self.handle_one_request()
File "C:\Users\aniket.bote\Desktop\Aniket\DS_2021_Array_cable_optimiz.py", line 374, in handle_one_request
self.run_wsgi()
File "C:\Users\aniket.bote\Desktop\Aniket\DS_2021_Array_cable_optimiz.py", line 319, in run_wsgi
execute(self.server.app)
File "C:\Users\aniket.bote\Desktop\Aniket\DS_2021_Array_cable_optimiz.py", line 310, in execute
for data in application_iter:
File "C:\Users\aniket.bote\Desktop\Aniket\DS_2021_Array_cable_optimiz_init__.py", line 309, in debug_application
app_iter = self.app(environ, start_response)
File "C:\Users\aniket.bote\Desktop\Aniket\DS_2021_Array_cable_optimizine 2088, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Users\aniket.bote\Desktop\Aniket\DS_2021_Array_cable_optimiz return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
File "c:\Users\aniket.bote\Desktop\Aniket\DS_2021_Array_cable_optimization\app.py", line 33, in test
file_logger.info(f"{i}")
Message: '24'
Arguments: ()
The log file generated contains numbers up to 22. I am trying to create a logger that will create a new log file once the buffer size is reached.生成的日志文件最多包含 22 个数字。我正在尝试创建一个记录器,一旦达到缓冲区大小,它将创建一个新的日志文件。 The code provided is a standalone code that regenerates the issue.
提供的代码是重新生成问题的独立代码。
I am using a windows system.我正在使用Windows系统。
The same code works if I remove the '.log' extension from the file如果我从文件中删除“.log”扩展名,则相同的代码有效
If you look at the traceback, there is this line如果你看回溯,有这一行
PermissionError: [WinError 32] The process cannot access the file becauUsers\\\\aniket.bote\\\\Desktop\\\\Aniket\\\\DS_2021_Array_cable_optimization\\\\iket.bote\\\\Desktop\\\\Aniket\\\\DS_2021_Array_cable_optimization\\\\logs\\\\deb
This implies that the path is wrong here, which can also be seen from becauUsers which is probably not a correct path in Windows.这意味着这里的路径是错误的,这也可以从becauUsers中看出,这在 Windows 中可能不是正确的路径。 You have to find where it uses this path.
您必须找到它在哪里使用此路径。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.