Reputation: 2148
I would like to create a file (preferably using with open(…)
) and have the owner be a different person than the person running the code.
I have tried to create the file and use os.chown
to change the owner, but that doesn't seem to work. Here is what I tried:
import os
import pwd
user=pwd.getpwnam('user')
with open('somefile', 'w') as f:
f.write('blah, blah')
os.chown('somefile', user.pw_uid, user.pw_gid)
I get the following error:
OSError: [Errno 1] Operation not permitted: 'somefile'
I would have thought that that was the correct way to change the owner of a file.
Upvotes: 1
Views: 5286
Reputation: 448
the problem is that you are not creating the new file as root, which is required. check out the comments to this question in SO. should solve the problem.
UPDATE: you need super user privileges to do this. so when running your program, use
sudo python yourprogramname.py
this will allow your python script super user privileges.
Upvotes: 1