Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 15th, 2004, 06:54 AM
pyton pyton is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 45 pyton User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
Files

Hi all

I want to compare two files and copy the access to a third file can some one show me how to ?

i.e.

File 1 has something like
abcde bcdef cdefg

File 2 has something like
abcde bcdef cdefg defgh

File 3 should be
defgh

Reply With Quote
  #2  
Old June 15th, 2004, 08:04 AM
DevCoach DevCoach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2004
Location: London, England
Posts: 1,221 DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 1 Week 5 Days 22 h 21 m 4 sec
Reputation Power: 263
I think you mean 'excess', not 'access'.

The problem you give is not well defined. You assume that file 2 is the same as file 1 with some extra data on the end. What happens if the two files are entirely different - what do you put in file 3? What do you do if file 2 is the same as file 1, but with additional data inserted in the middle? Or if file 2 is the same as file 1 but with some data removed?

Anyway, take a look at the difflib module in the standard library. It will let you do text comparisons and generate context diffs. This should provide everything you need, once you are clear about what your need is.

Dave - The Developers' Coach

Reply With Quote
  #3  
Old June 15th, 2004, 08:49 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,536 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 3 m 4 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
Daves right as usual, but working with the info you've given and assuming that file 2 will be file 1 with some excess then you can do it really easily.

Code:
#!/usr/bin/env python

page1 = file('file1.txt', 'r').read()
page2 = file('file2.txt', 'r').read()

file('file3.txt', 'w').write(page2.replace(page1, ''))


This isn't going to be too efficent if the files are massive but for most things this should be file. Basically what it does is removes the common bit in the first page from the second

You can also do it like this, as sugested by Grim.

Code:
#!/usr/bin/env python

page1 = file('file1.txt', 'r').read()
page2 = file('file2.txt', 'r').read()

file('file3.txt', 'w').write(page2[len(page1):])


the down side to this being it doesnt actually do any comparison to check if the two parts of the file are the same though this would be easy enough to add. But then again, using the info you gave, theres no real need to do a comparison since we're assuming that both file start of the same .

You might also want to take a look at filecmp or difflib in Pythons standard library:

http://www.python.org/doc/2.3.4/lib/module-filecmp.html
http://www.python.org/doc/2.3.4/lib/module-difflib.html

Hope this helps,

Mark.
__________________
programming language development: www.netytan.com Hula


Last edited by netytan : June 15th, 2004 at 09:10 AM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Files


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT