I want to extract two keywords of interest from a log file and calculate the time difference between them. Each line in the log starts with a timestamp in the following format. For example, I want to calculate the duration between the keywords abc and def.
01-20 09:18:42.637 721 4768 I abc : START a
01-20 09:18:43.968 721 777 I hhhhh : fffffff
01-20 09:19:02.886 11719 11864 D def :end cccc
duration_List.append(float("%.3f" % (start_time.timestamp() - start_time.timestamp()))) ^^^^^^^^^^^^^^^^^^^^^^ OSError: [Errno 22] Invalid argumen
my code is
def Get_Line_Time_s(line):
time_format = "%m-%d %H:%M:%S.%f"
dt = datetime.strptime(line[:18], time_format)
return dt
def Calculate_Time(logcat_file, keyword_start, keyword_end):
cmd = "grep -aEi '%s|%s' %s" % (keyword_start, keyword_end, logcat_file)
print("\n Using command: %s" % cmd)
tmp_file = TEMP_FILE
if os.path.exists(tmp_file):
os.remove(tmp_file)
with open(tmp_file, "w") as tmp:
subprocess.run(["bash", "-c", cmd], stdout=tmp)
tmp.close()
logs = open(tmp_file, "r")
read_lines = logs.readlines()
duration_List = []
pre_line = None
for line in read_lines:
if line.find(keyword_end) != -1 and pre_line != None:
end_time = Get_Line_Time_s(line)
print(end_time)
start_time = Get_Line_Time_s(pre_line)
duration_List.append(float("%.3f" % (end_time.timestamp() - start_time.timestamp())))
pre_line = line
logs.close()
return duration_List
I am a new Python programmer, and I encountered this error but don’t know how to troubleshoot it. Could you please provide some guidance and help? Thank you very much!
It fixed by change code as follows @Vũ Trí Anh Hoàng
def Get_Line_Time_s(line):
time_format = "%m-%d %H:%M:%S.%f"
dt = datetime.strptime(line[:18], time_format)
current_year = datetime.now().year
dt = dt.replace(year=2025)
return dt
I want to extract two keywords of interest from a log file and calculate the time difference between them. Each line in the log starts with a timestamp in the following format. For example, I want to calculate the duration between the keywords abc and def.
01-20 09:18:42.637 721 4768 I abc : START a
01-20 09:18:43.968 721 777 I hhhhh : fffffff
01-20 09:19:02.886 11719 11864 D def :end cccc
duration_List.append(float("%.3f" % (start_time.timestamp() - start_time.timestamp()))) ^^^^^^^^^^^^^^^^^^^^^^ OSError: [Errno 22] Invalid argumen
my code is
def Get_Line_Time_s(line):
time_format = "%m-%d %H:%M:%S.%f"
dt = datetime.strptime(line[:18], time_format)
return dt
def Calculate_Time(logcat_file, keyword_start, keyword_end):
cmd = "grep -aEi '%s|%s' %s" % (keyword_start, keyword_end, logcat_file)
print("\n Using command: %s" % cmd)
tmp_file = TEMP_FILE
if os.path.exists(tmp_file):
os.remove(tmp_file)
with open(tmp_file, "w") as tmp:
subprocess.run(["bash", "-c", cmd], stdout=tmp)
tmp.close()
logs = open(tmp_file, "r")
read_lines = logs.readlines()
duration_List = []
pre_line = None
for line in read_lines:
if line.find(keyword_end) != -1 and pre_line != None:
end_time = Get_Line_Time_s(line)
print(end_time)
start_time = Get_Line_Time_s(pre_line)
duration_List.append(float("%.3f" % (end_time.timestamp() - start_time.timestamp())))
pre_line = line
logs.close()
return duration_List
I am a new Python programmer, and I encountered this error but don’t know how to troubleshoot it. Could you please provide some guidance and help? Thank you very much!
It fixed by change code as follows @Vũ Trí Anh Hoàng
def Get_Line_Time_s(line):
time_format = "%m-%d %H:%M:%S.%f"
dt = datetime.strptime(line[:18], time_format)
current_year = datetime.now().year
dt = dt.replace(year=2025)
return dt
Because you don't have year in your datetime object
from datetime import datetime
time_str = "01-21 15:30:45.123456"
time_format = "%m-%d %H:%M:%S.%f"
dt = datetime.strptime(time_str, time_format)
new_dt = dt.replace(year=2025)
print(new_dt.timestamp()) # 1737448245.123456
print(dt.timestamp()) # Error Invalid argument
OSError
from a float cast. – AKX Commented Jan 21 at 8:13