2

Whenever I'm trying to run this python code, it returns a blank result / empty result. Can anyone help me with understanding why this happens?

#!/bin/python

import re
import time
import io

timecheck = open("/tmp/some.log", "r")
storeout = open("/tmp/storeout.txt", "w+")

for line in timecheck:
    if re.match("(.*)(Alarm obtained - type: KPI_CALCULATION)(.*)", line):
        out1 = line
        print >> storeout, line,

time1 = out1[11:19]
time2 = out1[164:172]
content = storeout.read()
print(content)
storeout.close()
2
  • 2
    please give us a log part Commented Nov 23, 2020 at 13:48
  • 1
    log file contains the below entry: 2020-11-23 13:10:57.596 petrosocial-esp-shutdown-handler DEBUG c.p.e.s.s.ShutdownAlarmConsumerService:54 - Alarm obtained - type: KPI_CALCULATION, time: 2020-11-23T12:51:00.000Z, KPI alarm type: KPI_CALCULATION 2020-11-23 13:12:31.245 petrosocial-esp-shutdown-handler DEBUG c.p.e.s.s.ShutdownAlarmConsumerService:54 - Alarm obtained - type: KPI_CALCULATION, time: 2020-11-23T13:12:25.000Z, KPI alarm type: KPI_CALCULATION Commented Nov 23, 2020 at 13:54

1 Answer 1

2

When you write to a file, the current file position is located at the end of the file. If you call read without explicitly setting the file position, you'll get back an empty result because you're already at the end of the file.

To read the data you've already written, you need to instruct Python to start reading from the beginning of the file using the seek method:

storeout.seek(0)
content = storeout.read()

Alternately, you can close the file and then re-open it for reading.

Sign up to request clarification or add additional context in comments.

3 Comments

Can anyone help with how this output can be sent via email.
You should probably post a new question if you want help with that.
Done, can you please take a look stackoverflow.com/questions/64971241/…

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.