我有一个要输入到.txt文件的变量“ inputed_email”。但是,您将如何在Flask中完成此任务?任何帮助表示赞赏。谢谢!
@app.route('/', methods=['GET', 'POST'])
def my_form():
inputed_email = request.form.get("email")
if request.method == 'POST' and inputed_email:
# code that writes "inputed_email" to a .txt file
return render_template('my-form.html', email=inputed_email)
return render_template('my-form.html')
答案 0 :(得分:1)
写入文件与Flask无关。您可以学习如何读写文件here。 请记住,写入大量数据很慢,并且会降低请求的速度。
if request.method == 'POST' and inputed_email:
# open is a built-in function, w is for write-mode (default is read)
with open('workfile', 'w') as f:
f.write(inputed_email)
return render_template(....)