Untitled
unknown
plain_text
a year ago
1.6 kB
1
Indexable
Never
# Finding the line with index as line number try: last_line_output = linecache.getline(vm_log_filename, last_line_no) print("Content for nth line :", last_line_output) except Exception as e: print("Exception while reading line from file:", e) return None filename = f"{vm_ip}-T{current_time}.txt" print(filename) X = int(old_line_number) # Starting line number #print(f"Old Value is {X}") Y = int(last_line_no) # Ending line number print(f'New Value is {Y}') try: with open(vm_log_filename, 'r') as input_file, open(filename, 'w') as output_file: # Using itertools.islice to copy lines between start and end indexes output_file.writelines(itertools.islice(input_file, X - 1, Y)) except Exception as e: print("Exception while copying lines to file:", e) return None # Updating the last index in a Bookmark File print("Updating the last index in a Bookmark File") bookmark_record = syslog_record print(f"Bookmark Record for {i} is {bookmark_record}") print(f"Updating Bookmark record for {bookmark_record}") updated_bookmark_rec = f"{bookmark_record}{last_line_no}\n" print(updated_bookmark_rec) try: with open(bookmark_file, 'r+') as file: lines = file.readlines() file.seek(0) for line_no, line in enumerate(lines): if bookmark_record in line: lines[line_no] = updated_bookmark_rec file.seek(0) file.writelines(lines)