Reputation: 3483
I have a script that needs to be run as a super user:
$ sudo ./python-script.py
Within this script, I do some other things that do not require super user privileges.
$ os.mkdir('somefolder')
What is the best/most efficient way of creating the directory as non-root user? Should I let the script make the directory as root user, and then change permissions on it?
Upvotes: 1
Views: 1071
Reputation: 92657
os.mkdir
does allow you to specify the permissions explicitly:
os.mkdir(path [, mode=0777])
And you also have the option of running os.chown
to set the user and group
os.chown(path, uid, gid)
You can probably get the original user like this (but it might be platform specific?)
import os
original_user = os.environ.get('SUDO_USER')
original_uid = os.environ.get('SUDO_UID')
original_gid = os.environ.get('SUDO_GID')
Upvotes: 3