This code that i've done sometimes deletes the entire file, can someone tell me what's the problem and solve it?
from flask import Flask
from flask.globals import request
app = Flask(__name__)
@app.route('/claim/<code>')
def index(code):
  with open('codes.txt') as file:
    for i in file:
      if i.strip() == str(code):
       with open('codes.txt', 'w') as newfile:
         for x in file:
           if x != str(code):
             newfile.write(x)
         return "VALID"
  return "INVALID"
if __name__ == "__main__":
  app.run(debug=True)```