[Tutor] Comparing lines in two files, writing result into a
third file
Terry Carroll
carroll@tjc.com
Sat Apr 26 15:19:02 2003
On Wed, 23 Apr 2003 stuart_clemons@us.ibm.com wrote:
> I have two files, file1.txt and file 2. txt, each containing numbers. I
> want to compare the numbers in the files and put an asterisk at the end of
> the numbers that match, writing the result into a third file, .
If the files are small enough, one of the easiest ways would be to read
each file into a sequence, then use difflib.differ() to produce a third
sequence that highlights the differences.
Then you can write that sequence out to a file, perhaps after reformattnig
it from differ()'s format to your own.
There's also a filecmp.cmp() method, which compares files, but
unfortunately, it only tells you whether they're equal or not,
without telling why they're different.
Another approach might be to invoke the OS's diff command, and process the
output.
--
Terry Carroll | "To have this rare opportunity
Santa Clara, CA | is a rare opportunity."
carroll@tjc.com | - Houston Rockets' Yao Ming, on being named
Modell delendus est | starting center for the 2003 NBA All-Star Game