为了账号安全,请及时绑定邮箱和手机立即绑定

如何将控制台输出写入 Flask 中的文本文件?

如何将控制台输出写入 Flask 中的文本文件?

莫回无 2021-07-26 12:46:14
我正在使用 Ubuntu、python3 和 Flask 服务器。我只想将打印在终端上的行 print('**found file', file.filename, '\n') 保存到文本文件中。任何形式的帮助将不胜感激。提前致谢。import osfrom flask import Flask, request, redirect, url_for, send_from_directory, jsonifyfrom werkzeug import secure_filenameUPLOAD_FOLDER = 'uploads'ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'mp4'])app = Flask(__name__)app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDERdef allowed_file(filename):  # this has changed from the original example because the original did not work for me    return filename[-3:].lower() in ALLOWED_EXTENSIONS@app.route('/', methods=['GET', 'POST'])def upload_file():    if request.method == 'POST':        file = request.files['file']        if file and allowed_file(file.filename):            print('**found file', file.filename, '\n')            filename = secure_filename(file.filename)            file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))            return jsonify({"success" : url_for('uploaded_file',                                    filename=filename)})    return '''    <!doctype html>    <title>Upload new File</title>    <h1>Upload new File</h1>    <form action="" method=post enctype=multipart/form-data>      <p><input type=file name=file>         <input type=submit value=Upload>    </form>    '''@app.route('/uploads/<filename>')def uploaded_file(filename):    return send_from_directory(app.config['UPLOAD_FOLDER'],                               filename)if __name__ == '__main__':    app.run(host= '0.0.0.0', debug=True)
查看完整描述

1 回答

  • 1 回答
  • 0 关注
  • 368 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信