로그가 기록 될 때 로그 파일의 마지막 줄을 읽는 코드 조각이 있습니다. 로그에 발생한 오류를 인쇄하려면 기본적으로 line.startswith('Error')
을 인쇄하고 line.startwith('End of Error')
일 때 인쇄를 시작하십시오. 내 코드는 아래에 있습니다. 아무도 이걸로 제발 도와 줄 수 없습니까?파이썬에서 로그 파일을 읽고 특정 텍스트를 출력합니다.
log = 'C:\mylog.log'
file = open(log, 'r')
res = os.stat(log)
size = res[6]
file.seek(size)
while 1:
where = file.tell()
line = file.readline()
if not line:
time.sleep(1)
file.seek(where)
else:
if line.startswith('Error'):
#print lines until you come to 'End of Error'
순간 무엇을 작동하지 않는 이유는 무엇입니까? 오류는 어디에 있습니까? 스크립트를 실행할 때 무엇을 얻으며 무엇을 기대 했습니까? –
"if line.startswith ('Error')"행 다음에 '오류 끝' –