0

I have a .log file in python. For example:

**** start logging ****
line 1
line 2
line 3
**** end logging ****
**** start logging ****
line 4
line 5
line 6
**** end logging ****

I want to read and print only the last logging of this file (when **** start logging **** is written). In our case:

**** start logging ****
line 4
line 5
line 6
**** end logging ****

As I know. the .read() and .seek() functions in python should be sufficient for this mission. But as I see, seek() only get offset, not a string variable.

How can I take only a specific part in this .log file?

1
  • 1
    did you think about regrex ? Commented Nov 20, 2014 at 14:05

4 Answers 4

2

You can use str.split

Demo

>>> content = '''**** start logging ****
... line 1
... line 2
... line 3
... **** end logging ****
... **** start logging ****
... line 4
... line 5
... line 6
... **** end logging ****'''
>>> ''.join(content.split('**** start logging ****')).replace('**** end logging ****','')
'\nline 1\nline 2\nline 3\n\n\nline 4\nline 5\nline 6\n'
Sign up to request clarification or add additional context in comments.

1 Comment

It'll be heavy on memory if the logfile is big, as your method requires the contents of the entire log file to be read at once.
1

you just need to split your file with **** start logging **** and then add it again to last element of split list !

with open('new.txt' ,'r') as f :
    lines=f.read()
    print "**** start logging ****"+ lines.split("**** start logging ****")[-1]

result :

**** start logging ****
line 4
line 5
line 6
**** end logging ****

Comments

0

You'll need to parse the entire file.

with open(logfile) as f:
    for line in f:
        if line.startswith('**** start logging'):
            l = []
        else:
            l.append(line)
print('\n'.join(l[:-1]))

The last entry in l will be the line end logging, so I explicitly remove it.

Alternatively, you could use file.seek(n, -2), where n is a guess for how many characters each logging record is, but then you would still have to perform some iteration to get the entire record, and check that there is only one instance of the phrase 'start logging' in it, which means you'll execute the code above anyway...

Comments

0

You speak of read and seek so I suppose you do not want to keep a full bunch of lines in memory. You can simply store the position of last logging start and rewind there :

with open(logfile) as fd:
    last_log_start = None
    while True:
        line = fd.readline()
        if line is None: break
        if line.startswith('**** start logging'):
            last_log_start = fd.tell()
    if last_log_start is not None:
        fd.seek(last_log_start)
        while True:
            line = fd.readline()
            if line is None: break
            if line.startswith('**** end logging'): break
            sys.stdout.write(line)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.