编程语言
首页 > 编程语言> > 如何在pythonanywhere中访问文件?

如何在pythonanywhere中访问文件?

作者:互联网

我尝试使用www.pythonanywhere.com构建Web应用程序,现在我创建了一个目录为“/> home> myid_xxx> mysite> FM”,在“FM”目录下,有两个文件:demo1.py和demo_pickle_file.pkl.

我尝试在’demo1.py’中cPickle.load(open(‘demo_pickle_file.pkl’,’rb’)),但是pythonanywhere控制台显示:

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

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

解决方法:

由于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)

标签:python,pythonanywhere
来源: https://codeday.me/bug/20190529/1175709.html