[英]How can you get the filepath of a file in flask?
I have a flask application which is utilizing Fabric to deploy code to certain machines. 我有一个烧瓶应用程序,它利用Fabric将代码部署到某些机器上。
One particular method, fabric.contrib.files.upload_template , requires a filename which is the path of a file I would like to upload to my remote machines. 一个特殊的方法, fabric.contrib.files.upload_template ,需要一个文件名 ,这是我想要上传到远程机器的文件的路径。
I have a file, index.php, which I want to place on my remote machines. 我有一个文件index.php,我想把它放在我的远程机器上。 I want to use upload_template to accomplish this task.
我想使用upload_template来完成这项任务。
Please note that: 请注意:
/home/Sparrowcide/flaskapp/path/to/file
won't work. /home/Sparrowcide/flaskapp/path/to/file
将无法正常工作。 /tmp/path/to/file
, but I'd rather not as this is not a very elegant solution. /tmp/path/to/file
的方法来逃避,但我不愿意,因为这不是一个非常优雅的解决方案。 Well the short answer is that you can pretty much put it wherever you want, I don't think flask will enforce any sort of structure on you here. 简而言之,你可以把它放在任何你想要的地方,我不认为烧瓶会在这里对你施加任何形式的结构。 However I personally would create a subdirectory called
data
and then in the flask app I might do something like this: 但是我个人会创建一个名为
data
的子目录,然后在烧瓶应用程序中我可能会这样做:
path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'data', 'index.php')
Or, if I felt I was likely to re-use this information (eg if i had multiple files to upload), I might break it out into multiple variables: 或者,如果我觉得我可能会重新使用这些信息(例如,如果我有多个文件要上传),我可能会将其分解为多个变量:
SRCDIR = os.path.dirname(os.path.abspath(__file__))
DATADIR = os.path.join(SRCDIR, 'data')
whatever_upload_function(os.path.join(DATADIR, 'index.php'))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.