read and delete only one line of a file?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
zmaier
Posts: 13
Joined: Thu May 16, 2019 12:44 pm

read and delete only one line of a file?

Post by zmaier » Thu Jul 04, 2019 7:36 am

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).

User avatar
jimmo
Posts: 802
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia

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

Post by jimmo » Thu Jul 04, 2019 8:04 am

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).

zmaier
Posts: 13
Joined: Thu May 16, 2019 12:44 pm

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

Post by zmaier » Thu Jul 04, 2019 11:42 am

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

Post Reply