Y
Y
Yrets1692022-01-12 18:14:51
linux
Yrets169, 2022-01-12 18:14:51

How to change the owner and permissions of a file using python?

Good afternoon, I'm trying to change the rights and user on a file in Linux with the following script:

#!/usr/bin/python3
import os, grp, pwd
uid = pwd.getpwnam("myuser").pw_uid
gid = grp.getgrnam("myuser").gr_gid
os.chmod("/tmpjcef-p11500_scheme.tmp", 0o777)
os.chown("/tmpjcef-p11500_scheme.tmp", uid,gid)


in response I receive

Traceback (most recent call last):
  File "/home/myuser/PycharmProjects/proj5/main.py", line 15, in <module>
    os.chmod("/tmpjcef-p11500_scheme.tmp", 0o777)
PermissionError: [Errno 1] Operation not permitted: '/tmpjcef-p11500_scheme.tmp'

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
light_and_ray, 2022-01-12
@light_and_ray

777 changes permissions for root too, so you need to run the script from under sudo to change it. Or you can look at other flags instead of 777 that do not require root rights

K
Kir Mozor, 2022-02-06
@150_Kirill_150

Operation not permitted

It says that you do not have rights to execute the command
, so run the script as root

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question