简体   繁体   English

如何获取烧瓶中文件的文件路径?

[英]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来完成这项任务。

Where should I place 'index.php' in my flask folder? 我应该将'index.php'放在我的烧瓶文件夹中? How will I be able to obtain the path of index.php for upload_template? 我怎样才能获得upload_template的index.php路径?

Please note that: 请注意:

  1. My application is hosted on Heroku so the typical /home/Sparrowcide/flaskapp/path/to/file won't work. 我的应用程序托管在Heroku上,因此典型的/home/Sparrowcide/flaskapp/path/to/file将无法正常工作。
  2. I know I can probably get away by invoking a method which writes to /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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM