Page 1 of 1

read and delete only one line of a file?

Posted: Thu Jul 04, 2019 7:36 am
by zmaier
Hello,

Code: Select all

with open(infile,'r') as ifile:
   for line in ifile:
      <do something with line>
      if everthing is iO
         delete line in file
      else   
	 leave line in file         	
How can i do that without using readlines and writelines (since the file is too big for the mem).

Re: read and delete only one line of a file?

Posted: Thu Jul 04, 2019 8:04 am
by jimmo
Do you have room on flash to make a new file?

i.e. with your code above, write out he current line to the new file (if you're keeping the line).

Then at the end, replace the old file with the new one. (os.unlink, os.rename).

Re: read and delete only one line of a file?

Posted: Thu Jul 04, 2019 11:42 am
by zmaier
Ok, thats a good workaround. I have enough room left, so this shouldn't be a problem.
I thought maybe there is a command which reads and deletes one line together.

THX