Writing to a .txt file in Flask
本问题已经有最佳答案,请猛点这里访问。
我有一个变量"inputed_email",我想将其写入.txt文件。然而,你们如何在烧瓶中完成这一点呢?感谢您的帮助。谢谢您!
1 2 3 4 5 6 7 8 9 | @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') |
写入文件与烧瓶无关。您可以在这里学习如何读/写文件。记住,大量数据的写入速度很慢,并且会减慢请求的速度。
1 2 3 4 5 | 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(....) |