简体   繁体   English

在pickle中存储数据时出现IO错误

[英]IO Error while storing data in pickle

i have a following code in a python to store data in pickle , but i am getting IO Error 我在python中有一个以下代码来存储pickle中的数据,但我得到IO错误

[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')

好吧,我分配了绝对路径,它工作了!!

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

我在Python 3.4中注意到你可以这样做:
output = open(str(dataList), "wb")

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM