In: Computer Science
Implement in Python a script that does the following:
1) reads input from a supposed file called firstnames_2.txt.
2) processes the input and writes and saves the output to a file.
NOTE:
Please make sure that
# this function read and write file
def read_and_write(ifilename, ofilename):
name_list=[]
# open file to read
fpi=open(ifilename,"r")
# read each lines
for line in fpi:
# content of line
name=line
# write the names along with the name length
name_list.append(name)
# close files
fpi.close()
# open file to write
fpo = open(ofilename, "w")
for i in range(len(name_list)):
fpo.write(name_list[i]);
fpo.write(str(len(name_list[i])-1));
fpo.write("\n");
fpo.close()
print("Output file has been written.")
# main function
def main():
input_filename = "firstnames_2.txt"
output_filename = "output.txt"
# call function to read and write input
read_and_write(input_filename, output_filename)
# driver code
if __name__ == "__main__":
main()
___________________________________________________________________
___________________________________________________________________
Alex
Tom
Hans
Roger
Harris
Robert
___________________________________________________________________
Alex
4
Tom
3
Hans
4
Roger
5
Harris
6
Robert
6
___________________________________________________________________
Note: If you have
queries or confusion regarding this question, please leave a
comment. I would be happy to help you. If you find it to be useful,
please upvote.