In: Computer Science
When the covid-19 situation arose in early 2020 your small team of developers quickly found out how much questions you used to ask each other in person. Now that you all work from home most of the time you have resorted to using online chat instead. But this has resulted in that you all have to go back and read endless chat logs when you forget what you agreed on earlier.
To remedy this problem, you have decided to write a small script that reads a log file and then allow you to search for all messages sent by one user. To make the script a bit more versatile you have decided that the path to the log file shall be sent as a command line argument to the script. As everyone in the team is a bit sloppy when typing, the script needs to have error handling that clearly informs the user in case the specified path to the log file does not exist. The format of the error message can be seen in this example:
Error: The file ‘/Users/nna/messages.txt’ could not be found.
The script shall have a main function, a function for reading from the log file, and a function for formatting and displaying a message on the screen. There are some rules for how the file-reading function and the display function shall work:
read_file(filename)
Shall read the file specified by the parameter filename and create
a list of tuples that is then returned. Each tuple shall consist of
a name and a message. In the file each message takes up two rows,
where the first row contains the senders name, and the second row
contains the text of the message. Each log file can of course
contain many messages sent by many different persons.
display_entry(name, message)
Shall use the two parameters to display a formatted output on the
screen. The output shall follow this format:
[name] --> message
All other functionality needed in the script shall be added in the main function, this includes but is not limited to asking what name to search the log file for.
PLEASE GIVE IT A THUMBS UP, I SERIOUSLY NEED ONE, IF YOU NEED ANY MODIFICATION THEN LET ME KNOW, I WILL DO IT FOR YOU
import sys
import os
def read_file(filename):
messages = []
f = open(filename, 'r')
lines = f.readlines()
temp_list = [0, 1]
for i in range(len(lines)):
if(i % 2 == 0):
temp_list[0] = lines[i].strip().lower()
else:
temp_list[1] = lines[i].strip()
messages.append(tuple(temp_list))
return messages
def display_entry(name, message):
print(name+"-->"+message)
try:
messages = read_file(sys.argv[1])
name = input("Enter the name to search:").lower()
count = 0
print("Messages are:")
for i in messages:
if(i[0] == name):
display_entry(*i)
count += 1
if(count == 0):
print("There are no messages with entered name in the logs")
except:
print("File error!!")
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
message.txt
Barry
Hello im a frontend developer
Harry
Hello im a backend developer
Barry
Hello team today a fixed a small glitch in the front end
Sally
Hello im the system administrator
Ben
Hello im server administrator
Barry
our server admin our companies web server is down please check it out
Harry
hello system admin please change my system password
Sally
Hey i changed your password and sent you a message please check it out.
Nora
Hello im sales manager
Nora
hello system admin can you create an account for me in the system
Sally
Ok Nora wait i will create you an account