I have to delete string or list of strings based on user input from a file. I referred the below link and things are working fine.
Deleting a specific line in a file (python)
However, the above approach reads the existing file contents in memory and if the line to delete is not found writes it back in the same file. This approach is not suitable if we dealing with files with huge amount of confidential data.
All i wish to know is, Is there a better way to do the same thing.
valid_List=["10.1.2.3","10.2.3.4","10.2.4.5","10.2.3.7"]
filename="abc.txt"
for i in valid_List:
f = open(filename,"r")
lines = f.readlines()
f.close()
f = open(filename,"w")
for line in lines:
if line!=i+" "+ "ok"+"\n":
#print("Writing ip not to be deleted")
f.write(line)
else:
print(i," Deleted")
user_response.append(i+" Deleted")
logger.info('Response returned to user%s',user_response)
f.close()