Python Delete Row In File After Reading It
I python 2.7 I am reading data from file in while loop. When I successfully read row, I would like to delete this row from a file, but I dont know how to do it - Efficient way so i
Solution 1:
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 (wherex
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:
while1:
pos = fp.tell() # remember the starting position of the next line to read
line = fp.readline()
ifnot line:
break# we reached the end of the fileif 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
Post a Comment for "Python Delete Row In File After Reading It"