[Errno 1] Operation not permitted - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: [Errno 1] Operation not permitted (/thread-27308.html) |
[Errno 1] Operation not permitted - whois1230 - Jun-02-2020 Hello, I'm trying to run the following program, but I'm getting an error. My OS is Linux Ubuntu 18.04 LTS and the IDE is IDLE. #!/usr/bin/python3.8 import os, sys # Open a file path = "/home/user/Desktop/Programming/Python/var/www/html/foo.txt" fd = os.open(path, os.O_RDWR|os.O_CREAT) # Close opened file os.close( fd ) # Now change the file ownership # Set a file owner id os.lchown( path, 500, -1) # Set a file group ID os.lchown( path, -1, 500) print("Changed ownership successfully!!") # "/home/user/Desktop/Programming/Python/var/www/html/foo.txt"The error is:
RE: [Errno 1] Operation not permitted - bowlofred - Jun-02-2020 Python is asking the OS to complete the lchown(), but the OS said the operation failed. Usually, this means you don't have permission to do what you're trying to do. If you are not already uid 500, and you are not root, then I would expect the lchown() call to fail as you've shown. RE: [Errno 1] Operation not permitted - whois1230 - Jun-04-2020 (Jun-02-2020, 08:49 PM)bowlofred Wrote: Python is asking the OS to complete the lchown(), but the OS said the operation failed. Usually, this means you don't have permission to do what you're trying to do.How can I get permission to run the program as root? RE: [Errno 1] Operation not permitted - bowlofred - Jun-04-2020 Easiest is to invoke the program with sudo .$ sudo python3 myscript.py
RE: [Errno 1] Operation not permitted - whois1230 - Jun-05-2020 (Jun-04-2020, 03:41 PM)bowlofred Wrote: Easiest is to invoke the program with user@Lenovo-ideapad-110-17IKB:~$ sudo python3 /home/user/Desktop/Programming/Python/Os lchown method.py [sudo] password for user: python3: can't open file '/home/user/Desktop/Programming/Python/Os': [Errno 2] No such file or directory user@Lenovo-ideapad-110-17IKB:~$ sudo python3 /home/user/Desktop/Programming/Python/Os lchown method.py python3: can't open file '/home/user/Desktop/Programming/Python/Os': [Errno 2] No such file or directory user@Lenovo-ideapad-110-17IKB:~$ RE: [Errno 1] Operation not permitted - ibreeden - Jun-05-2020 There are spaces in the filename. So you should quote the path: sudo python3 "/home/user/Desktop/Programming/Python/Os lchown method.py" (One hint when on Linux: Never use spaces in a filename. (It complicates your programs.)) RE: [Errno 1] Operation not permitted - knackwurstbagel - Jun-05-2020 Not sure how much this really helps your current problem but seeing you are using python 3.8 you might consider switching to the use of from pathlib import Path as it enables you to do cool things like for example from pathlib import Path our_file = Path("postgres.cast") with our_file.open() as fd: print(our_file.owner()) print(our_file.group()) for line in fd: print(line)and has the benefit of automatically closing the file, as well as other useful methods. In addition it is multiplatform, it understand paths from various operating systems. Not that this solves your current problem, just a pointer to something you might want to look into python shutil with path RE: [Errno 1] Operation not permitted - whois1230 - Jun-11-2020 (Jun-05-2020, 02:33 PM)ibreeden Wrote: There are spaces in the filename. So you should quote the path: user@Lenovo-ideapad-110-17IKB:~$ sudo python3 "/home/user/Desktop/Programming/Python/Os tcsetgrp method.py" [sudo] password for user: Current working dir :/home/user the process group associated is: 20025 Traceback (most recent call last): File "/home/user/Desktop/Programming/Python/Os tcsetgrp method.py", line 12, in <module> os.tcsetpgrp(fd,2672) PermissionError: [Errno 1] Operation not permitted user@Lenovo-ideapad-110-17IKB:~$ su Password: root@Lenovo-ideapad-110-17IKB:/home/user# sudo python3 "/home/user/Desktop/Programming/Python/Os tcsetgrp method.py" Current working dir :/home/user the process group associated is: 20065 Traceback (most recent call last): File "/home/user/Desktop/Programming/Python/Os tcsetgrp method.py", line 12, in <module> os.tcsetpgrp(fd,2672) PermissionError: [Errno 1] Operation not permitted root@Lenovo-ideapad-110-17IKB:/home/user# exit exit user@Lenovo-ideapad-110-17IKB:~$ RE: [Errno 1] Operation not permitted - ibreeden - Jun-12-2020 Good! One step further, you can start your program as root. But still you are not happy because the result is not what you want. chown is an old Unix program to change the owner of a file you own. Only root was allowed to change all ownerships. Later it appeared smart guys could misuse this so the use of chown was blocked for normal users. Later more security rules were added like:
So do you encounter the same problem when you choose a file outside the "var/www" directory? Of course there are ways around the problem by relaxing the security measures, but is that the best solution? So consider: why do you want to change the ownership of the file? Is it necessary? |