How to access a file in pythonanywhere?

I am trying to use www.pythonanywhere.com to create a web application, now I have created a directory like "/ > home > myid_xxx > mysite > FM", and under 'FM'there are two files: demo1.pyand demo_pickle_file.pkl.

I try 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 access the pickle file correctly?

+4
source share
1 answer

Since the pickle file is in the same directory as demo1.py, you can use something like the following to find its path from the script path:

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)
+6
source

All Articles