为什么要使用flask open_resource [英] Why use flask open_resource

查看:55
本文介绍了为什么要使用flask open_resource的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读flask api文档时,我遇到了这样的open_resource方法,该方法打开文件

while reading flask api documentation, I came across this open_resource method that opens file, like this

with app.open_resource('schema.sql') as f:
contents = f.read()
do_something_with(contents)

但是为什么不这样做呢?

but why not just do this?

with open('schema.sql') as f:
contents = f.read()
do_something_with(contents)

我想看看一个用例,其中app.open_resource可以执行open尚未执行的操作

I want to see a use case where app.open_resource could do something that open can't already do

推荐答案

来自

从应用程序的资源文件夹中打开资源.

Opens a resource from the application’s resource folder.

使用 app.open_resource ,路径始终相对于应用程序的根(资源)文件夹.它们只能打开以供读取,因为能够在生产环境中写入应用程序文件会很糟糕.

With app.open_resource, paths are always relative to the app's root (resource) folder. They may only be opened for reading, since it would be bad to be able to write to application files in production.

使用 open ,相对路径相对于当前目录.文件可以以任何方式打开.

With open, relative paths are relative to the current directory. Files may be opened in any mode.

这篇关于为什么要使用flask open_resource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆