473,387 Members | 1,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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
6 6819
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Lad | last post by:
Hi, What is the best method for comparing two files by words? I was thinking about reading files by words and compare them but a word in one file can be linked with a new line character ( \n) and...
2
by: SP | last post by:
Hi All, I need to compare two files line by line and copy the differing lines to a new files. I.e. Compare file1 and file2 line by line. Copy only differing lines to file3. I tried a couple...
0
by: . | last post by:
http://daviderognoni.blogspot.com?locawapp - MAIN NEWS =========== * add thread * add "Request" object * new "locawapp_main" function * fixed files.py
3
by: shona | last post by:
Hi, can any one told me how to compare files with same name but different extension.. for eg. if a.txt & a.doc then ans is same files.. Thanks
6
by: xkenneth | last post by:
Looking to do something similair. I'm working with alot of timestamps and if they're within a couple seconds I need them to be indexed and removed from a list. Is there any possible way to index...
4
by: Clay Hobbs | last post by:
I am making a program that (with urllib) that downloads two jpeg files and, if they are different, displays the new one. I need to find a way to compare two files in Python. How is this done? ...
0
by: norseman | last post by:
Timothy Grant wrote: =================================== If you are on a Unix platform: man cmp man identify man display (ImageMagick) gimp If you use mc (MidnightCommander) the F3 key can...
0
by: zw | last post by:
Hi I have 2 log files, each with a timestamp on the first 2 fields. However, when I do a awk '/ / {print $1,$2}' logs/x.log on a log file, it is complicated by the fact that I also get other...
3
by: Susan StLouis | last post by:
I'm writing a program that can be used to compare files. The program features a select that contains a list of files. After selecting several of the files. a "Biggest" button can be pushed to find...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.