【问题标题】:all time result is same in send_from_diretorysend_from_directory 中的所有时间结果都相同
【发布时间】:2019-09-23 06:48:18
【问题描述】:

当我运行这段代码时,它运行良好。 但是,当我将文件名从 happy_flow_logs.txt 更改为 1.txt 时,它总是将我返回到 happy_flow_logs.txt 文件。如何根据更新的文件名更改响应。

from flask import Flask, send_file, make_response, send_from_directory

app = Flask(__name__)

@app.route('/')
def download():
try:
    print("calling api")
    return send_from_directory(directory="/home/einfochips/Desktop/python/exercise", filename="happy_flow_logs.txt")
except FileNotFoundError as e:
    return e.strerror

if __name__ == '__main__':
    app.run()
    print("done")

【问题讨论】:

    标签: python api flask pytest


    【解决方案1】:

    您是否关闭了正在运行的烧瓶应用程序?

    您可以参考https://www.reddit.com/r/flask/comments/36ngb7/af_how_can_i_interrupt_an_infinite_while_loop_on/ 找到方法中断flask 应用程序并再次运行。

    如果缓存问题,请检查send_from_directory()方法中的cache_timeout选项。

    https://flask.palletsprojects.com/en/1.1.x/api/#flask.send_from_directory

    【讨论】:

    • 是的,我关闭了烧瓶应用程序,实际上我重新启动了我的系统,但仍然存储了缓存,我得到了旧文件。
    • 我想,你仍然没有得到我的问题,我的 cose 并没有进入无限循环,但是当我更新我的代码(如文件名更改)时,它不会反映在输出中,所有时间较旧的输出,我得到了。
    【解决方案2】:

    与 send_file() 相同 我们可以使用:

    return send_from_directory(directory ="/home/einfochips/Desktop/python/exercise",
                                    filename = "happy_flow_logs.txt",
                                    as_attachment = True,
                                    cache_timeout = 0)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-26
      • 1970-01-01
      • 2018-01-03
      • 2018-08-27
      • 2013-11-02
      • 1970-01-01
      • 2019-10-29
      • 1970-01-01
      相关资源
      最近更新 更多