首页 > 解决方案 > 如何在 python 中找到文件的一行并删除该行?

问题描述

我所做的这段代码有时会删除整个文件,有人可以告诉我问题出在哪里并解决吗?

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)```

标签: python

解决方案


推荐阅读