Reputation: 31
I have a log file which is building while a certain code is running. There exists a time step in this file that is changing and is important for my data analysis, since in each time step another file is overwritten. What I need is to have a python code which generate a few random integer for time steps let say 5 integer. I want to write a code to continuously monitor the log file and for the mentioned time step copy the overwritten file to certain directory.
Let me summarize it:
the log file contains some lines as the following 1 blablabla 2 blablabla 3 blablabla . . . 1000 blablabla
there is a file which will be overwritten in each step and I need to keep a few of them randomly. I want to save the corresponding created file of the random number in a directory while the main code is running and the log file is changing.
This is what I tried so far
def read_file():
with open("log file", "r") as f:
SMRF1 = f.readlines()
return SMRF1
import os #import library
import shutil #import library
import sys #import library
import numpy as np
lower_limit = 500
upper_limit = 1000
size = 5
X1 = np.random.randint(lower_limit, upper_limit, size=(5,))
#print(type(X1))
selected_steps = np.sort(X1)
print(selected_steps)
os.mkdir("mkdir") #make a directory
with open('log file') as f:
if 'random_time_step' in f.read():
shutil.copy2("f1", "mkdir") #f1 is the file which will be overwritten in each step
but it is not monitoring the log file during the execution time. How to force it to watch for changes in log file?
Any help is highly appreciated.
Upvotes: 2
Views: 702
Reputation: 85
you can use generator to monitor changes in file. checkout this example.
import time
def follow(thefile):
thefile.seek(0,2) # Go to the end of the file
while True:
line = thefile.readline()
if not line:
time.sleep(0.1) # Sleep briefly
continue
yield line
Upvotes: 1