I would like to create log file for each iteration of my script. I am creating logger for each iteration and i need close this opened file at the end of iteration because if I am not, I will recieve error Too many open files
.
formatter = logging.Formatter('%(asctime)s %(message)s')
handler = logging.FileHandler(logging_file)
handler.setFormatter(formatter)
logger = logging.getLogger(name)
logger.setLevel(logging.INFO)
logger.addHandler(handler)
handler.close()
This should work but I recieved SyntaxError:
File "start.py", line 73
handler.close()
^
SyntaxError: invalid syntax
How should I close file handlers at the end of iteration? I tried to create handler and close it immediately to be sure there is no problem that I added handler to logger before, but the problem persist:
handler = logging.FileHandler(logging_file)
handler.close()