The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Compaer files
Discuss Compaer files in the Python Programming forum on Dev Shed. Compaer files Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

May 21st, 2004, 06:15 AM
|
|
Contributing User
|
|
Join Date: May 2004
Posts: 45
Time spent in forums: < 1 sec
Reputation Power: 10
|
|
|
Compaer files
Hello all
I am trying to compare 2 files and print the (contents) difference do anyone know how to ?
file1
file2
file3=difference
|

May 21st, 2004, 06:40 AM
|
|
Contributing User
|
|
Join Date: Feb 2004
Location: London, England
|
|
Yes it is possible - in fact in Python it is astonishingly easy.
However the answer depends on the type of files you are passing in, and the format of the difference you are expecting out. I will assume that you want to compare text files, and get the output as a context diff then you can do it easily with the difflib module that was added in Python 2.1. This can show you the lines that were different, and even where in the line the difference is.
For example, from the docs:
Code:
>>> text1 = ''' 1. Beautiful is better than ugly.
... 2. Explicit is better than implicit.
... 3. Simple is better than complex.
... 4. Complex is better than complicated.
... '''.splitlines(1)
>>> text2 = ''' 1. Beautiful is better than ugly.
... 3. Simple is better than complex.
... 4. Complicated is better than complex.
... 5. Flat is better than nested.
... '''.splitlines(1)
>>> d = Differ()
>>> result = list(d.compare(text1, text2))
>>> import sys
>>> sys.stdout.writelines(result)
1. Beautiful is better than ugly.
- 2. Explicit is better than implicit.
- 3. Simple is better than complex.
+ 3. Simple is better than complex.
? ++
- 4. Complex is better than complicated.
? ^ ---- ^
+ 4. Complicated is better than complex.
? ++++ ^ ^
+ 5. Flat is better than nested.
The only potential issue is that the differ needs arrays of strings, so you need to read the entire files into memory. This may cause problems if the files are huge.
Here is a program that will do what you ask (I think - I have not tested it).
Code:
import sys
import difflib
first = file(sys.argv[1]).readlines()
second file(sys.argv[2].readlines()
diff = difflib.ndiff(first, second)
for line in diff:
print rstrip(line)
There is a more robust version of this program that comes with Python - see python/tools/scripts/ndiff.py.
Dave - The Developers' Coach
|

May 21st, 2004, 07:36 AM
|
|
Contributing User
|
|
Join Date: May 2004
Posts: 45
Time spent in forums: < 1 sec
Reputation Power: 10
|
|
|
Compare files
I shall try it now
Thank you dave
This site is great
|

May 21st, 2004, 07:59 AM
|
|
Contributing User
|
|
Join Date: May 2004
Posts: 45
Time spent in forums: < 1 sec
Reputation Power: 10
|
|
|
Compare File
Quote:
Hello Dave
I tried and i am getting the following error
Traceback (most recent call last):
File "F:/Pyth2/FileDirbinary10.py", line 11, in -toplevel-
print rstrip(line)
NameError: name 'rstrip' is not defined
I don't know if my coding is correct is there any other way to open a file from the netwoked server and copy or append to my local disk. Please help i am a beginner.
Here is my code
Code:
import os,sys,difflib
os.chdir('/')
os.chdir('\\\\server1')
openconsole=open('CONSOLE.LOG','rb',)
readconsole=openconsole.read()
os.chdir('c:\\')
local=open('console.log','rb',)
local1=local.read()
diff=difflib.ndiff(readconsole,local1)
for line in diff:
print rstrip(line)
local.close()
openconsole.close()
|
Thanks
|

May 21st, 2004, 09:07 AM
|
|
Contributing User
|
|
Join Date: Feb 2004
Location: London, England
|
|
Quote: | Originally Posted by pyton I tried and i am getting the following error
Traceback (most recent call last):
File "F:/Pyth2/FileDirbinary10.py", line 11, in -toplevel-
print rstrip(line)
NameError: name 'rstrip' is not defined |
Whoops, my mistake. It should have been
Code:
print line.rstrip()
I did say it was untested code!
Dave - The Developers' Coach
|

May 21st, 2004, 09:27 AM
|
|
Contributing User
|
|
Join Date: May 2004
Posts: 45
Time spent in forums: < 1 sec
Reputation Power: 10
|
|
|
Compare Files
Sorry
I tried again but i am not getting what i want
I want to disply only the difference between file1 and file2.
(say file one has 10 lines and file2 has same as file1 but 3 extra lines and I want the programme to pick only the extra 3 lines not everything from file1 and file2)
Thanks
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|