Computer Science I Compare Two Files CSCI-141 Homework 1 Task In this assignment
ID: 3753532 • Letter: C
Question
Computer Science I Compare Two Files CSCI-141 Homework 1 Task In this assignment you will compare two different files in Python. The comparison will be a line by line and character by character on each line. Write a function, char by_char, that will compare two files character by character and print the umber of characters that are different. This is not a true full-file comparison. You will actually compare a line from one file to the line at the same location in the other file, character by character. Addition or removal of whole lines will not be detected. 1.1 Requirements Your function should take in two file names as parameters; assume both files exist. The char_by_char function will: . Read the next line from each file. If the lines are different lengths, report only the line umber and proceed to the next line in each file. If the lines are the same length, compare the lines character by character. . If the lines match completely, proceed to the next line in each file. . If they do not match, then for each line: Print Unmatched characters:' as a line label; and -print the number of the line and the first character position that does not match. Print this line information in N: M format, and use 1-based character positions (see examples); Then proceed to process the next line in each file. - .If one of the files has fewer lines than the other, the function reports the 'extra line(s) as non-matching. . Lastly, the function prints: - the number of characters in the first file; - the number of characters in the second file; -the number of characters that do not match in lines of the same length; and the number of lines that are not the same length. Note: Count all characters and spaces, but do not count newline characters. 1.2 Example Executions While out put does not have to match exactly, it should display the same information.Explanation / Answer
file1 = input("Enter first file path and name:")
#print(file1)
file2 = input("Enter second file path and name:")
#print(file2)
firstFile = open(file1, "r")
secondFile = open(file2, "r")
first_file_ended = False
second_file_ended = False
first_file_total_characters=0
second_file_total_characters=0
diff_length_count=0
current_line_number =0
unmatched_string=""
unmatch_count=0
while (True):
current_line_number +=1
first_file_line = firstFile.readline()
second_file_line =secondFile.readline()
if (len(first_file_line)==0):
first_file_ended=True
else:
#print(first_file_line)
first_file_total_characters = first_file_total_characters+ len(first_file_line)
if (len(second_file_line)==0):
second_file_ended=True
else:
#print(second_file_line)
second_file_total_characters =second_file_total_characters+ len(second_file_line)
if (first_file_line==second_file_line):
unmatched_string=unmatched_string
else:
if(len(first_file_line)==len(second_file_line)):
unmatch_count+=1
position_of_difference = [i for i in range(len(first_file_line)) if first_file_line[i] != second_file_line[i]]
unmatched_string = unmatched_string + str(current_line_number)+":"+ str(position_of_difference[0]+1)+", "
else:
unmatched_string=unmatched_string+str(current_line_number)
diff_length_count+=1
if(first_file_ended and second_file_ended):
break
print("Character by character:")
print("Unmatched characters :" + unmatched_string )
print("There are " + str(first_file_total_characters) + " characters in " +file1)
print("There are " + str(second_file_total_characters) + " characters in " +file2)
print("There are " + str(unmatch_count) + " unmatched characters in the files")
print("There are " + str(diff_length_count) +" lines of different length")
firstFile.close()
secondFile.close()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.