I am trying to write a program that compares two txt file to see if they are the same. The program is to promt the user for two txt files and then compare them. If they are the same it should print yes but if not it should print no followed by the first line of each file that is diffeent. I know how to promt the user for the filename but i do not know how to compare them.
How to compare two txt files to see if they are the same?
Collapse
X
-
-
-
-
I actually got that far on my own but thanks anyways. Can you help me figure out how to make it print the first line in each file that is different?Comment
-
-
This is what i have, but it won't run right.
Code:file1 = raw_input("What is the name of the first sile including the .txt?") file2 = raw_input("what is the name of the second file including the .txt?") file1 = open("file1" , "r") file2 = open("file2", "r") if file1 == file2: print("Yes") else: print("No")Comment
-
-
The file name is a text string and is assigned to the identifier fn1. An open file object is returned by built-in function open() and assigned to identifier fileobj1. Notice the identifier fn1 is the argument representing the file name.Code:fn1 = raw_input("Enter file name.") fileobj1 = open(fn1, 'r')Last edited by bvdet; Oct 31 '10, 01:41 PM.Comment
-
-
Here is what i have:
Code:fn1 = raw_input("What is the name of the first file (be sure to include .txt after the filename)?") fn2 = raw_input("What is the name of the second file (be sure to include .txt after the filename)?") fileobj1 = open("fn1", "r") fileobj2 = open("fn2", "r") if file1 == file2: print ("Yes these files are the same.") else: print("No these file are not the same.")Comment
-
You have to supply the complete path to the file, unless it is in PYTHONPATH, see here.Code:import sys print sys.path ## ## if the path to the file is not in the above then you have to provide the path fileobj1 = open("/path/to/fn1", "r") ## ## or add it to sys.path sys.path.append("/path/to") fileobj1 = open("fn1", "r")Comment
Comment