Python truncate lines as they are read

后端 未结 7 610
梦毁少年i
梦毁少年i 2020-12-17 17:10

I have an application that reads lines from a file and runs its magic on each line as it is read. Once the line is read and properly processed, I would like to delete the li

7条回答
  •  遥遥无期
    2020-12-17 17:37

    A related post has what seems a good strategy to do that, see How can I run the first process from a list of processes stored in a file and immediately delete the first line as if the file was a queue and I called "pop"?

    I have used it as follows:

      import os;
    
      tasklist_file = open(tasklist_filename, 'rw');  
      first_line = tasklist_file.readline();
      temp = os.system("sed -i -e '1d' " + tasklist_filename); # remove first line from task file;
    

    I'm not sure it works on Windows. Tried it on a mac and it did do the trick.

提交回复
热议问题