user1592380
user1592380

Reputation: 36227

Flask Project root directory on openshift

I'm getting started with flask and I'm deploying my app to openshift.

My apps code:

def get_users():
    f = open('./users.txt')
    ....

when I run:

$ rhc tail flaskpq

I see:

[Sun Jun 21 09:05:20 2015] [error] [client 127.2.78.1] f = open('./user.txt') [Sun Jun 21 09:05:20 2015] [error] [client 127.2.78.1] IOError: [Errno 2] No such file or directory: './users.txt'

My suspicion is that my relative reference to users.txt in my projects root directory is no longer valid on deployment to openshift. It works fine locally on win7. What is the best way to fix this?

Upvotes: 2

Views: 292

Answers (3)

Robert Moskal
Robert Moskal

Reputation: 22553

You should be able to do something like this to get the directory for the app:

user_file = os.path.abspath(os.path.join(os.path.dirname(__file__), "./users.txt"))

It's what I do on heroku. Don't forget ____file___ will be the directory the file resides in. This might or might not be your application root folder.

Upvotes: 2

user3134284
user3134284

Reputation:

Don't forget that on openshift you can create/write files only under the $OPENSHIFT_DATA_DIR so I personally use following kind of approach.

DATA_DIR = os.environ.get('OPENSHIFT_DATA_DIR', ".")
def get_users():
    f = open(os.path.join(DATA_DIR,'users.txt')

And I copy the file using rhc scp to correct place if needed.

Upvotes: 1

Gx1sptDTDa
Gx1sptDTDa

Reputation: 1588

You should generally not write paths in pure strings. This is dangerous when working cross-platform. Please use the functions in os and os.path.

Upvotes: 1

Related Questions