How to compare two txt files to see if they are the same?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • anderson1373
    New Member
    • Nov 2009
    • 23

    How to compare two txt files to see if they are the same?

    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.
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    For small files, both could be read into memory as strings and compared for equality as in:
    Code:
    >>> s1 = "abcdef"
    >>> s2 = "abcdef"
    >>> s1 == s2
    True
    >>> s3 = "xyz"
    >>> s1 == s3
    False
    >>>
    Alternatively, you can use module filecmp to compare files.

    Comment

    • anderson1373
      New Member
      • Nov 2009
      • 23

      #3
      i have import them in and then comare them! How do i do that?

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        Code:
        s1 = open(file_name1).read()
        s2 = open(file_name2).read()
        if s1 == s2:
            print "File contents are identical"
        else:
            print "File contents are different"

        Comment

        • anderson1373
          New Member
          • Nov 2009
          • 23

          #5
          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

          • anderson1373
            New Member
            • Nov 2009
            • 23

            #6
            to open the file is what you have the same as:
            infile = open ("filename" , "r")

            Comment

            • anderson1373
              New Member
              • Nov 2009
              • 23

              #7
              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

              • dwblas
                Recognized Expert Contributor
                • May 2008
                • 626

                #8
                For opening and reading files with a for() loop, read section 9.1 here.

                Comment

                • anderson1373
                  New Member
                  • Nov 2009
                  • 23

                  #9
                  The way i have it it says no such file or directory on line 4. How do i fix it?

                  Comment

                  • bvdet
                    Recognized Expert Specialist
                    • Oct 2006
                    • 2851

                    #10
                    Code:
                    fn1 = raw_input("Enter file name.")
                    fileobj1 = open(fn1, 'r')
                    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.
                    Last edited by bvdet; Oct 31 '10, 01:41 PM.

                    Comment

                    • anderson1373
                      New Member
                      • Nov 2009
                      • 23

                      #11
                      I tried this and it still says no such file or directory

                      Comment

                      • anderson1373
                        New Member
                        • Nov 2009
                        • 23

                        #12
                        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

                        • dwblas
                          Recognized Expert Contributor
                          • May 2008
                          • 626

                          #13
                          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

                          Working...