7

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')
Hunt
  • 8,215
  • 28
  • 116
  • 256
  • did you check write permissions of the data.pk1 file? – dting Mar 12 '11 at 20:15
  • 1
    Moar details! Obviously, the program doesn't have permission to open the file - but of course you already figured that out. So the question is why it doesn't have permissions - and the code doesn't have anything to do with *that*. –  Mar 12 '11 at 20:18
  • permissions are as follows -rw-r--r-- – Hunt Mar 12 '11 at 20:31
  • Try running it with a different file name, say `'data2.pkl'`, and then compare the permissions and ownership with that of `'data.pkl'`. – Steven Rumbalski Mar 12 '11 at 20:37
  • its a same sort of permission -rw-r--r-- , i tried changing it by running sudo chmod o+w data.pkl & sudo chmod g+w data.pkl command for data.pkl file after that it has -rw-rw-rw- but still getting the same permission denied error – Hunt Mar 12 '11 at 20:43

3 Answers3

10

Well i assigned the absolute path & it worked !!

output = open('/home/user/test/wsservice/data.pkl', 'wb')
Hunt
  • 8,215
  • 28
  • 116
  • 256
1

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

1

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()
abhijit
  • 79
  • 3