0

My flask app reads an locally stored excel file and it is running perfect on local machine.

When I deploy the same app on heroku it is throws exception-

FileNotFoundError: [Errno 2] No such file or directory: 'C:/QuestionBank_v5.xlsx'

Which is valid as heroku deployment cannot read from my local machine.

Is there a way to store this file along with flask app files and make it readable when deployed on heroku?

user2961127
  • 963
  • 2
  • 17
  • 29
  • You need to use relative path to file and not a static absolute path. Note that heroku server and your local system are too different machines at two different places. Check here : https://stackoverflow.com/questions/918154/relative-paths-in-python – N.Moudgil Feb 20 '20 at 05:12

1 Answers1

0

Yes, you can store the file in the same directory as the Flask app file and use os.path to get the app's current directory and append the file name to it.

import os

# Current directory for Flask app
APP_ROOT = os.path.dirname(os.path.abspath(__file__))

# Current directory for Flask app + file name
# Use this file_path variable in your code to refer to your file 
file_path = os.path.join(APP_ROOT, 'QuestionBank_v5.xlsx')

print(file_path)
Thaer A
  • 2,243
  • 1
  • 10
  • 14