Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

flask to write data into file

I have problem to write data into file from my flask developments server (win7),

 @app.route('/')
 def main():

    fo = open("test.txt","wb")
    fo.write("This is Test Data")

 return render_template('index.html')

Why this don't works in flask ?

like image 305
Beka Avatar asked Oct 31 '25 15:10

Beka


1 Answers

You should either flush the output to the file or close the file because the data may still be present in the I/O buffer.

Even better use the with statement as it'll automatically close the file for you.

with open("test.txt", "w") as fo:
   fo.write("This is Test Data")
like image 135
Ashwini Chaudhary Avatar answered Nov 02 '25 06:11

Ashwini Chaudhary



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!