|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
|
#2
|
|||
|
|||
|
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 |
|
#3
|
||||
|
||||
|
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. Last edited by netytan : June 15th, 2004 at 09:10 AM. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > Files |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|