简体   繁体   中英

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.

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.

I have a file, index.php, which I want to place on my remote machines. I want to use upload_template to accomplish this task.

Where should I place 'index.php' in my flask folder? How will I be able to obtain the path of index.php for upload_template?

Please note that:

  1. My application is hosted on Heroku so the typical /home/Sparrowcide/flaskapp/path/to/file won't work.
  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.

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:

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'))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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