Remove the end of a text file
I've a small problem in python:
I try to delete all the lines of a file after the last occurrence of a string (line where the string is included). I'm looking for a long time the approach to take to realize that when the module that could help me.
Is there anyone who can help me to remove/delete lines of a file?
Re: Remove the end of a text file
Do this:
1. create a new file,
2. copy all the old in the new except the end
3. delete the old file and rename the new file for it is the name of the former.
Re: Remove the end of a text file
Open your file, the line passes in itertools.takewhile writing the lines out into a new file, the last operation optional is to delete the original file and rename the new.
Code:
$ python
Python 2.6.1 (r261:67515, Jul 7 2009, 23:51:51)
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import itertools
>>> with open('as.txt','w') as f:
... f.writelines(itertools.takewhile(lambda line: not line.lower().startswith('b'), open('/usr/share/dict/words')))
...
>>> ^D
$ wc -l /usr/share/dict/words
234936 /usr/share/dict/words
$ wc -l as.txt
17061 as.txt
in as.txt I have words that start with "a".
Re: Remove the end of a text file
Problems deleting rows I already had to go through something like that, the problem I have is to detect the last occurrence of a string in the file.
I am reading the doc "re" but it does not seem to have what I want.
Re: Remove the end of a text file
This occurrence happens in all lines until it can no longer, or it is more rough estimates, like it can happen anywhere?
And the string to check, is what kind, what a head it has?
Re: Remove the end of a text file
It can happen anywhere, and it is not all lines. It looks like:
- 15:53:15 (logmount) TIMESTAMP 10/1/2010
As the string "TIMESTAMP" is the only specific to this line, I went to the last TIMESTAMP detect the file and delete this line, then all that follows.
Re: Remove the end of a text file
The line itself should not be included? If you do so
Code:
from itertools import dropwhile
with open('file.name.out', 'w') as output:
with open('file.name') as input:
output.writelines(reversed(
dropwhile(lambda line: 'TIMESTAMP' not in line,
reversed(input))))
then there are surely mistakes that are not tested at all, but it should be a thing of style.