error.log
also both snippets will fail if there is space in the path. if you expect these you may have to adjust the code accordingly
Output:2019-11-18 01:46:47:INFO:3496839:/var/logs/apc/ : no errors detected.
2019-11-18 01:46:47:INFO:3496839:/var/logs/xyz/ : no errors detected.
2019-11-18 01:46:47:ERROR:3496839:check reported errors in the /var/logs/jkl/ database. These should be rechecked to verify if the errors are accurate.
2019-11-18 01:46:47:ERROR:3496839:check reported errors in the /var/logs/jkl/spam database. These should be rechecked to verify if the errors are accurate.
using just str methodslog_file = 'error.log' with open(log_file) as lf: for line in lf: log_date_hour, log_minute, log_seconds, log_type, some_code, info, *rest = line.split(':') if log_type == 'ERROR': print(info.split(' ')[5])
Output:/var/logs/jkl/
/var/logs/jkl/spam
using regeximport re regex = re.compile(r'reported errors in the (?P<path>\S*)', flags=re.MULTILINE) with open(log_file) as lf: logs = lf.read() paths = regex.findall(logs) print(paths)
Output:['/var/logs/jkl/', '/var/logs/jkl/spam']
if file is huge it may be better to read it line by line (like in first example and use regex to parse the line). maybe it's possible to make better regex pattern thoughalso both snippets will fail if there is space in the path. if you expect these you may have to adjust the code accordingly
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs