如何在pythonanywhere中访问文件?

时间:2013-12-15 03:37:28

标签: python pythonanywhere

我尝试使用www.pythonanywhere.com构建Web应用程序,现在我创建了一个目录"/ > home > myid_xxx > mysite > FM",并在'FM'目录下有两个文件:{{1} }和demo1.py

我在'demo1.py'中尝试demo_pickle_file.pkl,但pythonanywhere控制台显示:

cPickle.load(open('demo_pickle_file.pkl','rb'))

那么,如何正确访问pickle文件?

1 个答案:

答案 0 :(得分:6)

由于pickle文件与demo1.py位于同一目录中,因此您可以使用以下内容从脚本路径中找到它的路径:

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)