11

I am trying to run a python script which uses a binary file (xFiles.bin.addr_patched) created by a postlinker. However, I am getting this error.

File "abc.py", line 74, in ParseCmd
shutil.copy(gOptions.inputX, gWorkingXFile)
File "/usr/lib/python2.6/shutil.py", line 89, in copy
copymode(src, dst)
File "/usr/lib/python2.6/shutil.py", line 66, in copymode
os.chmod(dst, mode)

OSError: [Errno 1] Operation not permitted: 'myPath/xFiles.bin.addr_patched'

When I checked the permissions of this xFiles.bin, by ls-l, it shows that

-rwxrwxrwx 1 nobody  nogroup 

I presume the error is because this file was created by some other application, the python script I am running does not have access to it. Since I am beginner wrt ubuntu, I don't really know how to fix it. Any suggestions on how to fix this?

SOLVED:

As one of the answers Suggested : chown username:groupname file name fixes this issue

user1357576
  • 389
  • 2
  • 3
  • 17

4 Answers4

14

You could try (from the command line, but I'm sure there's a syntax in python):

sudo chown your_username:your_groupname filename

Note: The group is usually just your username. I feel like there's something wrong with those permissions though. Read Write Execute for everyone seems to be off. How was this file created? How did it get to be created by the user nobody?

ForceBru
  • 43,482
  • 10
  • 63
  • 98
Linuxios
  • 34,849
  • 13
  • 91
  • 116
  • 1
    @user1357576: With the `sudo`? And *what* doesn't work? Do the permissions not change? Is there an error? What is the `ls -l filename` output? – Linuxios Jun 07 '12 at 18:47
  • -rwxrwxrwx 1 myUsername nogroup is the change. However, I meant it doesn't work. As in the error doesn't go away – user1357576 Jun 07 '12 at 18:59
  • What are the permissions for the folder `myPath`? – Linuxios Jun 07 '12 at 19:03
  • drwxrwxrwx is the permission for the path – user1357576 Jun 07 '12 at 19:09
  • @Linux_iOS.rb.cpp.c.lisp.m.sh I tried the edited script. Now I am the owner and the group is set as well but the error just wont go away. I am still facing the same issue. – user1357576 Jun 07 '12 at 21:39
  • @user1357576: What happens if you try something like `echo Bork > thefile`? – Linuxios Jun 07 '12 at 22:27
  • @Linux_iOS.rb.cpp.c.lisp.m.sh Thank you so much for your help. The script worked. I was so stupid. I was setting the wrong user name. basically I have a different login name and different user name I was making the wrong name the owner and so I wasn't able to fix the issue. Thank you all so much and sorry about my stupidity and wasting your time. In the end, alls well :) – user1357576 Jun 07 '12 at 22:34
  • We really shouldn't have to be changing ownerships on files just to copy (and quite often this won't be permissible anyway). Isn't the answer the same as http://stackoverflow.com/questions/11835833/why-would-shutil-copy-raise-a-permission-exception-when-cp-doesnt - to use shutil.copyfile() instead? – Joe Watkins Feb 17 '15 at 14:53
5

Python code to change the permission:

from getpwnam import pwd
from getgrnam import grp
import os

uid = getpwnam("YOUR_USERNAME")[2]
gid = grp.getgrnam("YOUR_GROUPNAME")[2]
os.chown("myPath/xFiles.bin.addr_patched", uid, gid)

Run the script with sudo and you're done.

Fatih Arslan
  • 16,499
  • 9
  • 54
  • 55
  • 2
    It isen't the best idea to run your own scripts with `sudo`... Especially if you don't know if they work. – Linuxios Jun 07 '12 at 18:52
  • @FatihArslan The thing is , I don't want to meddle with the python code because it is a standard application and making changes in this script can be a headache. I run an external shell script which calls the python script. Do you think I can make any changes in this shell script? – user1357576 Jun 07 '12 at 19:13
2

I had this problem when running a python script on my mac (10.14 Mojave) trying to access /Users/xxx/Pictures/Photos Library.photoslibrary. The full solution can be found in http://osxdaily.com/2018/10/09/fix-operation-not-permitted-terminal-error-macos/

Summary: Go to System Preferences > Security & Privacy > Privacy > Full Disk Access and add your IDE or python interpreter to the list.

Federico
  • 1,636
  • 2
  • 22
  • 24
  • Yes, this was the only solution because Apple changed (again) something to make it more uncomfortable. Cant wait till I get my Linux system. Thank you. – Coliban Oct 14 '20 at 10:31
1

My guess is that you should be looking at the permissions for myPath folder instead. Seems like you can't write to it, hence the problem. Try ls -l myPath/.. and see the permissions for myPath. If that's the problem, change the permissions on the folder with chmod.

P.S. Also, see Google top result on Linux file permissions.

Lev Levitsky
  • 63,701
  • 20
  • 147
  • 175