home > myid_xxx > mysite > FM", and under the " /> home > myid_xxx > mysite > FM", and under the " /> home > myid_xxx > mysite > FM", and under the "/>

How to access file in pythonanywhere?

6.2k Views Asked by At

I try to use www.pythonanywhere.com to build the web application, now I have create a directory as "/ > home > myid_xxx > mysite > FM", and under the 'FM' directory, there are two files : demo1.py and demo_pickle_file.pkl .

I try to cPickle.load(open('demo_pickle_file.pkl','rb')) in 'demo1.py', but the pythonanywhere console shows :

IOError: [Errno 2] No such file or directory: 'demo_pickle_file.pkl'

So, how to correctly access the pickle file ?

1

There are 1 best solutions below

4
martineau On BEST ANSWER

Since the pickle file is in the same directory asdemo1.pyyou could use something like the following to find the path to it from the path to the script:

import os
import cPickle as pickle

my_dir = os.path.dirname(__file__)
pickle_file_path = os.path.join(my_dir, 'demo_pickle_file.pkl')

with open(pickle_file_path, 'rb') as pickle_file:
    demo = pickle.load(pickle_file)