I want to open a file in Python.
For every line in the file split it with line.split(" "),
so it will access the first word.
Then check if the word is equal to a given string "TEST". If this is the case delete the line else just keep it.
I have tried using readlines() to read the lines. That worked, but then I couldn't figure out how to delete the entire line.
This is my code so far
def delete_client(client):
f = open("clients.md", "r")
data = f.readlines()
f = open("clients.md", "w")
for line_counter in range(len(data)):
splitted_line = data[line_counter].strip("\n").split()[0]
print("\n" + splitted_line)
if client == splitted_line:
print("Equal to client")
f.write("HELLO")
else:
print("Not equal to client")
f.close
delete_client("DELETE_ME")
The clients file
first 192.168.0.14 Lukas administrator
another text 192.168.0.14 Lukas administrator
DELETE_ME 192.168.0.14 Lukas administrator
fourth 192.168.0.14 Lukas administrator