IO Error while storing data in pickle

老子叫甜甜 提交于 2019-12-07 06:13:49

问题


i have a following code in a python to store data in pickle , but i am getting IO Error

[Errno 13] Permission denied: 'data.pkl'

Code

def SaveUserData(request):
       datalist={}
       datalist['empid']='127113'
       datalist['empname']='eric'
       datalist['empphone']='66335500'
       datalist['email']='eric.pk@moliba.com'
       output = open('data.pkl', 'wb')
       pickle.dump(datalist, output)
       output.close()
       data = simplejson.dumps(datalist, indent=4)
       return HttpResponse(data,mimetype='application/javascript')

回答1:


Well i assigned the absolute path & it worked !!

output = open('/home/user/test/wsservice/data.pkl', 'wb')



回答2:


I've noticed in Python 3.4 you can do it like:
output = open(str(dataList), "wb")




回答3:


In my case it was the problem with my current directory.

I have added the following lines to set the current working directory to my script directory.

Hope this will solve the problem if writing to the script directory do not need admin permission.

import sys, os

def getScriptPath():
    return os.path.dirname(os.path.realpath(sys.argv[0]))

print 'Current working directory : ', os.getcwd()
os.chdir(getScriptPath())
print 'Changed working directory : ', os.getcwd()


来源:https://stackoverflow.com/questions/5285181/io-error-while-storing-data-in-pickle

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!