0

Given the following piece of code, I'm looping through a file to process some data. When that data is processed I want to remove the item from the words list and move it to the complete list.

I'm able to populate the completed file but not remove the line from the initial file. Does anyone know the syntax to remove that current line?

I have had a search around already but cannot find a specific bit of syntax for this.

startFile = open("words.txt", "a")
completedFile = open("completed-words.txt", "a")

with open('words.txt') as f:
  for line in f:

    # do something with the line

    completedFile.write(line)
    print(line.rstrip() + " - complete")

startFile.close()
completedFile.close()
martineau
  • 119,623
  • 25
  • 170
  • 301
Shane Jones
  • 885
  • 1
  • 9
  • 27
  • I am afraid there is no "direct" solution, you basically need to rewrite the startFile skipping the relevant line – Giovanni Tardini Apr 21 '22 at 07:58
  • You have to open a third, temporary, file to which you write all the non-matched lines. At the end, you can overwrite the input file with that temporary file. If all lines match and are processed, you can just remove the input file (or better yet: leave it be. Why alter the input file if you have a separate output file with the completed data?) – 9769953 Apr 21 '22 at 08:00

1 Answers1

0

Consider the problem of adding bytes in the middle of a file. Each byte has a "location" within that file. If you want to add bytes without losing other bytes, you would need to copy all the bytes to the right of the "location" and shift them right. Similarly, if you are removing bytes from within the file, you will need to shift all the bytes to the right of the "location" leftward.

You do not want to be doing this for every line that you choose not to include.

You should create a new file that you write to, only writing the lines from the initial file that you want. You can then delete the initial file if you no longer need it. This is also safer than modifying the initial file directly as that runs into the risk of losing data by mistake should there be an error or typo in the processing code.

OTheDev
  • 2,916
  • 2
  • 4
  • 20