Cannot import modules 1. Write a program called diff.py that asks the user for t
ID: 662465 • Letter: C
Question
Cannot import modules
1. Write a program called diff.py that asks the user for two files and displays the differences
between the characters in each file.
1. Your program should ask the user for the path to two different files
2. For every character that is in the same position in each file but is different between the two
files your program should display the index of those characters (starting from 0) as well as
the value of those two characters.
3. If one file is longer than the other your program should report the index of each of the
characters as well as the fact that it could not find a matching character
4. Examples:
1. Assume ex1.txt contains the contents:
Explanation / Answer
# save it as diff.py
file1_name=raw_input('Enter the name of the first file: ')
file = open(file1_name, 'r')
x = file.read()
file2_name = raw_input('Enter the name of the second file: ')
file1 = open(file2_name, 'r')
y = file1.read()
for i in range(max(len(y),len(x))):
try:
if x[i]==y[i]:
pass
else:
print 'Mismatch at character %d %s!=%s' % (i,x[i],y[i])
except:
if len(x)>len(y):
print 'No matching character for character %d (%s) found in %s. ' % (i, x[i],file1_name)
else:
print 'No matching character for character %d (%s) found in %s. ' % (i, y[i],file2_name)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.