Write a program diff.py that takes two file names from the command line. The fil
ID: 3742836 • Letter: W
Question
Write a program diff.py that takes two file names from the command line. The file names may NOT have spaces in them! It then writes either files differ or files are the same.
Example: the user may run your program thus:
Method: open the files, and read their lines. Then compare their lists of lines.
CODE GIVEN:
'''
Takes two filenames from the command line, and determines
if the files have the same content.
'''
import sys
if len(sys.argv) != 3:
print('Usage: python diff.py <file1> <file2>')
sys.exit(1)
file_name1 = sys.argv[1]
# ... write the rest of your code here
Explanation / Answer
''' Takes two filenames from the command line, and determines if the files have the same content. ''' import sys print(sys.argv) if len(sys.argv) != 3: print('Usage: python diff.py ') sys.exit(1) file_name1 = sys.argv[1] file_name2 = sys.argv[2] same_files = True with open(file_name1, 'r') as f1, open(file_name2, 'r') as f2: lines1 = f1.readlines() lines2 = f2.readlines() print(lines1) print(lines2) if len(lines1) == len(lines2): for i in range(len(lines1)): if lines1[i] != lines2[i]: same_files = False break else: same_files = False if same_files: print('Files are same') else: print('Files are not same')Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.