'How to remove current line from file in a loop? [duplicate]
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()
Solution 1:[1]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | oda |
