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 dont waste to much of CPU.
read = open("data.csv", 'r')for row in read:#code......if send == True:-->delete sent line from file, and continue to loop
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 readline = fp.readline()if not 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 filefp.seek(pos) # go to the start position of the line to removefp.write(rest) # write the rest of the file over the line to be removedfp.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