Python Forum
"Cut" big log file according to wanted dates?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
"Cut" big log file according to wanted dates?
#1
Hello ,
I created a small program to cut line from log file
import sys

OutputFile = open('C:\\Users\\David\\Desktop\\OutPutLog.txt', "w+")
file1 = open('C:\\Users\\David\\Desktop\\Log.txt', 'r')
Lines = file1.readlines()
file1.close()

StartDate = input('Enter Start Date\n')
EndDate = input('Enter End Date\n')

count = 0
StartLine = 0
EndLine = 0
# Strips the newline character
for line in Lines:
    count += 1
    if StartDate in line and StartLine == 0:
        print("Start Line {}: {}".format(count, line.strip()))
        StartLine = count
    if EndDate in line and EndLine == 0:
        print("End Line{}: {}".format(count, line.strip()))
        EndLine = count
count = 0
print('start line is %d , end line is %d' % (StartLine, EndLine))
print('total number of line is  %d' % (EndLine-StartLine))
for line in Lines:
    count += 1
    if StartLine <= count <= EndLine:
        OutputFile.write(line.strip() + "\r\n")
it's working
but I feel I can write it better
can someone help with make the code better\smallest\ prettier?

**** also I have notice that if the file is bigger then 4GB - I get memory error
is there any way to overcome this problem ?

Thanks ,
Reply


Messages In This Thread
"Cut" big log file according to wanted dates? - by korenron - Apr-14-2021, 07:28 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020