467,077 Members | 1,022 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,077 developers. It's quick & easy.

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

Hi,

i have 2 files which are different (1 line difference):
$ diff groupresult20070226190027.xml groupresult20070226190027-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("groupresult20070226190027.xml", "groupresult20070226190027-2.xml")
1

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

Feb 27 '07 #1
  • viewed: 6543
Share:
6 Replies
On Feb 26, 10:09 pm, "ying...@gmail.com" <ying...@gmail.comwrote:
Hi,

i have 2 files which are different (1 line difference):
$ diff groupresult20070226190027.xml groupresult20070226190027-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("groupresult20070226190027.xml", "groupresult20070226190027-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.

Feb 27 '07 #2
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 groupresult20070226190027.xml groupresult20070226190027-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("groupresult20070226190027.xml", "groupresult20070226190027-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:
http://doc.astro-wise.org/difflib.html
it said it is just for comparing 2 sequence of strings, not files.

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

Feb 27 '07 #3
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 groupresult20070226190027.xml groupresult20070226190027-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("groupresult20070226190027.xml", "groupresult20070226190027-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.context_diff(f1.readlines(), f2.readlines()).len()
== 0):

But I get this error:
Traceback (most recent call last):
File "./scripts/regressionTest.py", line 72, in ?
runTest(savedPagesDirectory, outputDir, expectedResultDir,
failedDir);
File "./scripts/regressionTest.py", line 60, in runTest
getSnapShot(fileName, testNo, outputDir, expectedResultDir,
failedDir)
File "./scripts/regressionTest.py", line 30, in getSnapShot
if (difflib.context_diff(f1.readlines(), f2.readlines()).len() ==
0):
# no difference
else:
# files are different
AttributeError: 'generator' object has no attribute 'len'

Can you please help?

Feb 27 '07 #4
In <11*********************@8g2000cwh.googlegroups.co m>, yi*****@gmail.com
wrote:
File "./scripts/regressionTest.py", line 30, in getSnapShot
if (difflib.context_diff(f1.readlines(), 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
Feb 27 '07 #5
On Feb 27, 12:07 am, Marc 'BlackJack' Rintsch <bj_...@gmx.netwrote:
In <1172553141.665287.65...@8g2000cwh.googlegroups.co m>, ying...@gmail.com
wrote:
File "./scripts/regressionTest.py", line 30, in getSnapShot
if (difflib.context_diff(f1.readlines(), 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.
Feb 27 '07 #6
sa***************@gmail.com kirjoitti:
On Feb 27, 12:07 am, Marc 'BlackJack' Rintsch <bj_...@gmx.netwrote:
>In <1172553141.665287.65...@8g2000cwh.googlegroups.co m>, ying...@gmail.com
wrote:
>> File "./scripts/regressionTest.py", line 30, in getSnapShot
if (difflib.context_diff(f1.readlines(), 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.

I think you are mixing cmp and filecmp.cmp. They are two different beasts.

Cheers,
Jussi
Feb 27 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Lad | last post: by
2 posts views Thread by SP | last post: by
3 posts views Thread by shona | last post: by
4 posts views Thread by Clay Hobbs | last post: by
reply views Thread by norseman | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.