python - 如何在 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 - 到坐标点的距离矩阵
- php - spatie/laravel-permissions assignRole 方法导致错误 [完整性约束违规:1452 无法添加或更新子行]
- c# - ASP MVC 路由不匹配
- c# - 无法在新的 Chrome 浏览器选项卡中打开 pdf
- reactjs - 在 @connect 装饰器回调中存储参数
- python - Facebook API - 如何在提取之前访问视频见解?
- database - CrateIO 插入失败
- mysql - 导入数据时在mysql中quotechar是什么意思?
- c# - .NET Mutex 导致 Windows 窗体中的 GUI 线程饥饿
- neo4j - Neo4j:数字属性键