How to use cmp() function to compare 2 files?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • yinglcs@gmail.com

    #1

    How to use cmp() function to compare 2 files?

    Hi,

    i have 2 files which are different (1 line difference):
    $ diff groupresult2007 0226190027.xml groupresult2007 0226190027-2.xml
    5c5
    < x:22 y:516 w:740 h:120 area:
    ---
    x:22 y:516 w:740 h:1202 area:

    But when I use the cmp() function to compare 2 files, it return "1",

    $ python Python 2.4.3 (#1, Oct 23 2006, 14:19:47)
    [GCC 4.1.1 20060525 (Red Hat 4.1.1-1)] on linux2
    Type "help", "copyright" , "credits" or "license" for more information.
    >>cmp("groupres ult200702261900 27.xml", "groupresult200 70226190027-2.xml")
    1

    Can you please tell me why? And how can I compare the content of 2
    files in python?

  • Dan Bishop

    #2
    Re: How to use cmp() function to compare 2 files?

    On Feb 26, 10:09 pm, "ying...@gmail. com" <ying...@gmail. comwrote:
    Hi,
    >
    i have 2 files which are different (1 line difference):
    $ diff groupresult2007 0226190027.xml groupresult2007 0226190027-2.xml
    5c5
    < x:22 y:516 w:740 h:120 area:
    ---
    >
    x:22 y:516 w:740 h:1202 area:
    >
    But when I use the cmp() function to compare 2 files, it return "1",
    >
    $ python Python 2.4.3 (#1, Oct 23 2006, 14:19:47)
    [GCC 4.1.1 20060525 (Red Hat 4.1.1-1)] on linux2
    Type "help", "copyright" , "credits" or "license" for more information.>>c mp("groupresult 20070226190027. xml", "groupresult200 70226190027-2.xml")
    >
    1
    >
    Can you please tell me why?
    Because '.' '-' (character 25 of each string).

    But this is comparing the filenames, which isn't what you want to do.
    And how can I compare the content of 2
    files in python?
    Use the difflib module.

    Comment

    • yinglcs@gmail.com

      #3
      Re: How to use cmp() function to compare 2 files?

      On Feb 26, 10:22 pm, "Dan Bishop" <danb...@yahoo. comwrote:
      On Feb 26, 10:09 pm, "ying...@gmail. com" <ying...@gmail. comwrote:
      >
      >
      >
      Hi,
      >
      i have 2 files which are different (1 line difference):
      $ diff groupresult2007 0226190027.xml groupresult2007 0226190027-2.xml
      5c5
      < x:22 y:516 w:740 h:120 area:
      ---
      >
      x:22 y:516 w:740 h:1202 area:
      >
      But when I use the cmp() function to compare 2 files, it return "1",
      >
      $ python Python 2.4.3 (#1, Oct 23 2006, 14:19:47)
      [GCC 4.1.1 20060525 (Red Hat 4.1.1-1)] on linux2
      Type "help", "copyright" , "credits" or "license" for more information.>>c mp("groupresult 20070226190027. xml", "groupresult200 70226190027-2.xml")
      >
      1
      >
      Can you please tell me why?
      >
      Because '.' '-' (character 25 of each string).
      >
      But this is comparing the filenames, which isn't what you want to do.
      >
      And how can I compare the content of 2
      files in python?
      >
      Use the difflib module.
      Thanks, but from here:

      it said it is just for comparing 2 sequence of strings, not files.

      How can I use that to compare content of 2 files?

      Comment

      • yinglcs@gmail.com

        #4
        Re: How to use cmp() function to compare 2 files?

        On Feb 26, 10:53 pm, "ying...@gmail. com" <ying...@gmail. comwrote:
        On Feb 26, 10:22 pm, "Dan Bishop" <danb...@yahoo. comwrote:
        >
        >
        >
        On Feb 26, 10:09 pm, "ying...@gmail. com" <ying...@gmail. comwrote:
        >
        Hi,
        >
        i have 2 files which are different (1 line difference):
        $ diff groupresult2007 0226190027.xml groupresult2007 0226190027-2.xml
        5c5
        < x:22 y:516 w:740 h:120 area:
        ---
        >
        x:22 y:516 w:740 h:1202 area:
        >
        But when I use the cmp() function to compare 2 files, it return "1",
        >
        $ python Python 2.4.3 (#1, Oct 23 2006, 14:19:47)
        [GCC 4.1.1 20060525 (Red Hat 4.1.1-1)] on linux2
        Type "help", "copyright" , "credits" or "license" for more information.>>c mp("groupresult 20070226190027. xml", "groupresult200 70226190027-2.xml")
        >
        1
        >
        Can you please tell me why?
        >
        Because '.' '-' (character 25 of each string).
        >
        But this is comparing the filenames, which isn't what you want to do.
        >
        And how can I compare the content of 2
        files in python?
        >
        Use the difflib module.
        >
        I try this:
        f1 = open("f1",'r')
        f2 = open("f2", 'r')


        if (difflib.contex t_diff(f1.readl ines(), f2.readlines()) .len()
        == 0):

        But I get this error:
        Traceback (most recent call last):
        File "./scripts/regressionTest. py", line 72, in ?
        runTest(savedPa gesDirectory, outputDir, expectedResultD ir,
        failedDir);
        File "./scripts/regressionTest. py", line 60, in runTest
        getSnapShot(fil eName, testNo, outputDir, expectedResultD ir,
        failedDir)
        File "./scripts/regressionTest. py", line 30, in getSnapShot
        if (difflib.contex t_diff(f1.readl ines(), f2.readlines()) .len() ==
        0):
        # no difference
        else:
        # files are different
        AttributeError: 'generator' object has no attribute 'len'

        Can you please help?

        Comment

        • Marc 'BlackJack' Rintsch

          #5
          Re: How to use cmp() function to compare 2 files?

          In <1172553141.665 287.65280@8g200 0cwh.googlegrou ps.com>, yinglcs@gmail.c om
          wrote:
          File "./scripts/regressionTest. py", line 30, in getSnapShot
          if (difflib.contex t_diff(f1.readl ines(), f2.readlines()) .len() ==
          0):
          # no difference
          else:
          # files are different
          AttributeError: 'generator' object has no attribute 'len'
          >
          Can you please help?
          The function returns a generator/iterator over the differences which has
          no `len()` method. If you just want to know if two files are equal or not
          use `filecmp.cmp()` . Read the docs about the `shallow` argument of that
          function.

          Ciao,
          Marc 'BlackJack' Rintsch

          Comment

          • samuel.y.l.cheung@gmail.com

            #6
            Re: How to use cmp() function to compare 2 files?

            On Feb 27, 12:07 am, Marc 'BlackJack' Rintsch <bj_...@gmx.net wrote:
            In <1172553141.665 287.65...@8g200 0cwh.googlegrou ps.com>, ying...@gmail.c om
            wrote:
            >
            File "./scripts/regressionTest. py", line 30, in getSnapShot
            if (difflib.contex t_diff(f1.readl ines(), f2.readlines()) .len() ==
            0):
            # no difference
            else:
            # files are different
            AttributeError: 'generator' object has no attribute 'len'
            >
            Can you please help?
            >
            The function returns a generator/iterator over the differences which has
            no `len()` method. If you just want to know if two files are equal or not
            use `filecmp.cmp()` . Read the docs about the `shallow` argument of that
            function.
            >
            Ciao,
            Marc 'BlackJack' Rintsch
            Thanks. I use that before, it does not work for me, since it always
            return 1, regardless if the file content of 2 files are different or
            not.


            Comment

            • Jussi Salmela

              #7
              Re: How to use cmp() function to compare 2 files?

              samuel.y.l.cheu ng@gmail.com kirjoitti:
              On Feb 27, 12:07 am, Marc 'BlackJack' Rintsch <bj_...@gmx.net wrote:
              >In <1172553141.665 287.65...@8g200 0cwh.googlegrou ps.com>, ying...@gmail.c om
              >wrote:
              >>
              >> File "./scripts/regressionTest. py", line 30, in getSnapShot
              >> if (difflib.contex t_diff(f1.readl ines(), f2.readlines()) .len() ==
              >>0):
              >> # no difference
              >> else:
              >> # files are different
              >>AttributeErro r: 'generator' object has no attribute 'len'
              >>Can you please help?
              >The function returns a generator/iterator over the differences which has
              >no `len()` method. If you just want to know if two files are equal or not
              >use `filecmp.cmp()` . Read the docs about the `shallow` argument of that
              >function.
              >>
              >Ciao,
              > Marc 'BlackJack' Rintsch
              >
              Thanks. I use that before, it does not work for me, since it always
              return 1, regardless if the file content of 2 files are different or
              not.
              >
              >
              I think you are mixing cmp and filecmp.cmp. They are two different beasts.

              Cheers,
              Jussi

              Comment

              Working...