问题
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