I wrote a script to search a keyword in a logs, which is working perfectly fine. but the thing is if i want to find another string i have to make a clone and make it for the other one. i am automating my test cases so i can not wait for the window to show up and ask me what keyword i want to search so i was hoping if there is any way i can just write my keyword with python script in capl function. so that python script remains same.
import os
import glob
import datetime
import sys
def get_latest_dlt_file(folder_path):
# Get a list of all .dlt files in the folder
dlt_files = glob.glob(os.path.join(folder_path, "*.dlt"))
# Filter out directories, get modification times, and sort by modification time
dlt_files = [(f, os.path.getmtime(f)) for f in dlt_files if os.path.isfile(f)]
dlt_files.sort(key=lambda x: x[1], reverse=True)
# Return the path of the latest modified .dlt file, if any
return dlt_files[0][0] if dlt_files else None
def dlt_to_txt_converter(dlt_file_path, txt_file_path):
try:
with open(dlt_file_path, 'r', encoding='utf-8', errors='ignore') as dlt_file:
dlt_content = dlt_file.read()
with open(txt_file_path, 'w', encoding='utf-8') as txt_file:
txt_file.write(dlt_content)
print(f"Conversion successful! Contents have been written to {txt_file_path}")
except Exception as e:
print(f"An error occurred: {e}")
def search_string_line_by_line(txt_file, search_string):
found = False
try:
try:
with open(txt_file, 'r', encoding='utf-8', errors='ignore') as file:
print(f"Opened file: {txt_file} with utf-8 encoding")
for line in file:
if search_string in line:
found = True
break
except UnicodeDecodeError:
with open(txt_file, 'r', encoding='latin1', errors='ignore') as file:
print(f"Opened file: {txt_file} with latin1 encoding")
for line in file:
if search_string in line:
found = True
break
with open("DLT_Message.txt", "w", encoding='utf-8') as message_found:
if found:
message_found.write("1")
print("String found in the text file.")
else:
message_found.write("0")
print("String not found in the text file.")
except FileNotFoundError:
print(f"File not found: {txt_file}")
except Exception as e:
print(f"An error occurred: {e}")
if __name__ == "__main__":
# Specify the folder containing the .dlt files
folder_path = r"C:SoftwarePython"
# Get the path of the latest modified .dlt file in the folder
latest_dlt_file = get_latest_dlt_file(folder_path)
if latest_dlt_file:
# Define the path for the converted .txt file
txt_file_path = os.path.join(os.path.dirname(latest_dlt_file), os.path.splitext(os.path.basename(latest_dlt_file))[0] + ".txt")
# Define the search string
search_string = "ALT-INFO"
# Convert the latest .dlt file to .txt
dlt_to_txt_converter(latest_dlt_file, txt_file_path)
# Search for the string in the .txt file
search_string_line_by_line(txt_file_path, search_string)
else:
print("No .dlt files found in the specified folder.")
# Close the terminal
os._exit(0)
CAPL
void Search_Logs()
{
sysExecCmd("search_logs.py", "", putty_pythonScriptPath);
}
Muhammad Faizan Bukhari is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.