Deleting lines from a file

Michael Bentley michael at jedimindworks.com
Mon Dec 17 09:53:44 EST 2007


On Dec 17, 2007, at 6:25 AM, Horacius ReX wrote:

> and regardless of the speed, what do you think would be the best
> method to do this ?


The first thing I'd look into is reading the whole file into memory,  
making all the deletions, and finally writing it out.  But you said  
the file is big, so here's a quick stab at it (with multiple read  
passes and a single write):

import string
rm = []

#first pass through file -- mark some lines for deletion
for line, text in enumerate(file('words')):
     if text[0] in string.uppercase:
         rm.append(line)

#second pass -- mark lines with 'e' for deletion
for line, text in enumerate(file('words')):
     if line in rm:
         print 'skipping %s' % line
         continue
     if 'e' in text:
         rm.append(line)

# now write the modified file
print 'Writing %d of %d lines' % (len(rm), line)
outFile = file('newWords', 'w')
for line, text in enumerate(file('words')):
     if line not in rm:
         outFile.write(text)

hth,
Michael

---
Simplicity is the ultimate sophistication. -Leonardo da Vinci






More information about the Python-list mailing list