-2

I want to read one text file line by line and delete the line. So steps are like below.

  1. Read the line
  2. Delete the line

Repeat step 1 and 2 until the file is not empty

For example data in file is like below.

1,2,3,4,5
a,b,c,d,e
q,w,e,r,t
a,s,d,f,g

So, steps will be ... while Reading

Read 1,2,3,4,5
Delete 1,2,3,4,5

then next line

Read a,b,c,d,e
Delete a,b,c,d,e

..
..
and so on until the file is not empty.

I know Python well, but I am not able to do it.

halfer
  • 19,824
  • 17
  • 99
  • 186
Tarun Khaneja
  • 451
  • 11
  • 23
  • 5
    That's extremely expensive, since you have to rewrite the whole file to delete a line (i.e. deleting a line from a file = copying the file line-by-line and neglecting to write one of them, then moving the new file over the old file). Would reading each line in the file then deleting everything work for you? – Amadan Oct 03 '18 at 07:41
  • 1
    Did you try something? – Danila Ganchar Oct 03 '18 at 07:42
  • I wonder why don't you just read all the lines one by one and create an empty file in the end? – d3corator Oct 03 '18 at 07:43
  • Why do you need to delete one line at a time? – Peter Wood Oct 03 '18 at 07:46
  • 4
    Your urgency is not shared by us, and certainly not an excuse for you to put no effort into solving this yourself. – Mad Physicist Oct 03 '18 at 07:46
  • @Amadan Yeah, that's why I am looking for some efficient way. and the thing is while reading line and processing it one by one... if error raise in middle ... So, I don't want to reprocess complete file again. That's why I want to delete line whichever lines i have read – Tarun Khaneja Oct 03 '18 at 08:43
  • @DanilaGanchar I tried using truncate.. but it clears the complete file – Tarun Khaneja Oct 03 '18 at 08:43
  • @MuhammadAsif Because while processing data line by line if any error raise... So next time i don't want to reprocess complete file from starting – Tarun Khaneja Oct 03 '18 at 08:44
  • @PeterWood Wood Because while processing data line by line if any error raise... So next time i don't want to reprocess complete file from starting – Tarun Khaneja Oct 03 '18 at 08:45
  • @MadPhysicist If ihave mentioned urgent keyword... that doen't mean I didn't try anything and am directly asking on stackoverflow – Tarun Khaneja Oct 03 '18 at 08:46
  • You could keep a record of which line you have processed up to, and continue from there on following attempts. – Peter Wood Oct 03 '18 at 08:48
  • @MadPhysicist The quesion you are reffering is different than what I asked – Tarun Khaneja Oct 03 '18 at 08:49
  • @Tarun. Given what you've asked and the amount of code you show, I believe that it very much is. SO is not a free coding site. – Mad Physicist Oct 03 '18 at 08:54
  • 1
    Please read [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](//meta.stackoverflow.com/q/326569) - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. – halfer Oct 03 '18 at 08:57

1 Answers1

0

Don't even try!

At its lowest level, a file is a sequential stream of bytes. You can read bytes from that stream, overwrite bytes, and position your cursor (position where to read of write) anywhere between the beginning and the end of a file. When you are at end of file and write, you extend the file. You can also truncate a file (remove everything past the cursor).

So there is no way to remove the initial part of a file! It is commonly done by writing a new file containing what needs to be kept remove the original file and rename the new file to give it the original name.

I assume that you are trying the wrong tool to solve your real problem...

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252