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".
Bookmarks