Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting more info from filecmp

Status
Not open for further replies.

starlite79

Technical User
Aug 15, 2008
89
US
Hello,

I am using the cmp function of the module filecmp interactively as follows:

Code:
import filecmp
filecmp.cmp('file1', 'file2')
If they're the same, the result is true, otherwise false.

If the files are not the same, is there a way to get line numbers where the files differ or something with more information than just False?
 
Justin,

I looked at the difflib module and found a function called ndiff. I tried to use it as follows:

Code:
>>> import difflib
>>> difflib.ndiff('test.case', 'tidesmod.test')
<generator object at 0x2ab44efaa878>

What does that mean? In the example I used, the files are the same. Could someone give an example of how to use the difflib module for file comparison? I don't want to match keywords or patterns in files.
 
difflib works on lists of strings so you'll have to read in the lines yourself

also try
>>> help(difflib) and
>>> help(deifflib.ndiff)
in your interactive prompt

ndiff(a, b, linejunk=None, charjunk=<function IS_CHARACTER_JUNK>)
Compare `a` and `b` (lists of strings); return a `Differ`-style delta.

Optional keyword parameters `linejunk` and `charjunk` are for filter
functions (or None):

- linejunk: A function that should accept a single string argument, and
return true iff the string is junk. The default is None, and is
recommended; as of Python 2.3, an adaptive notion of "noise" lines is
used that does a good job on its own.

- charjunk: A function that should accept a string of length 1. The
default is module-level function IS_CHARACTER_JUNK, which filters out
whitespace characters (a blank or tab; note: bad idea to include newline
in this!).

Tools/scripts/ndiff.py is a command-line front-end to this function.

Example:

>>> diff = ndiff('one\ntwo\nthree\n'.splitlines(1),
... 'ore\ntree\nemu\n'.splitlines(1))
>>> print ''.join(diff),
- one
? ^
+ ore
? ^
- two
- three
? -
+ tree
+ emu

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top