You shouldn't concern yourself about cpu usage when doing disk IO -- disk IO is very slow compared to almost any in-memory/cpu operation.
There are two strategies to deleting from the middle of a file:
writing all lines to keep to a secondary file, then renaming the secondary file to the original file name.
copy the rest (tail) of the file to the beginning of the line you want to delete and then truncating x
bytes off the end of the file (where x
is equal to the length of the line you want to remove.
Number 1 is usually preferred since it is easier and doesn't require any locking.
Mayank Porwal has given you most of strategy #1. Here is how you would implement strategy #2:
# open the file for both reading and writing in binary mode ('rb+')
with open('rmline.txt', 'rb+') as fp:
while 1:
pos = fp.tell() # remember the starting position of the next line to read
line = fp.readline()
if not line:
break # we reached the end of the file
if should_line_be_skipped(line): # only you know what to skip :-)
rest = fp.read() # read the rest of the file
fp.seek(pos) # go to the start position of the line to remove
fp.write(rest) # write the rest of the file over the line to be removed
fp.truncate() # truncates at current position (end of file - len(line))
fp.seek(pos) # return to where the next line is after deletion so we can continue the while loop
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…