0

I have a variable "inputed_email" that I want to write to a .txt file. However, how would you accomplish this in Flask? Any help is appreciated. Thank you!

@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')
Evan Yang
  • 125
  • 1
  • 8

1 Answers1

1

Writing to a file has nothing to do with Flask. You can learn how to read/write to files here. Keep in mind writing a large amount of data is slow, and will slow down the request.

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(....)
4d11
  • 307
  • 1
  • 11
  • I tried this, however I got error 500 (internal server error) on my website when submitted the form that gets inputed_email. Why is this? – Evan Yang Jun 20 '18 at 21:11
  • You need to provide more information, could you post your error trace? Also, try logging `inputed_email` to see if you are actually getting a valid value. – 4d11 Jun 20 '18 at 21:12
  • Sounds good, I'll work on getting the error trace and logging it. I'll post them in a bit. – Evan Yang Jun 20 '18 at 21:20