Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a files owner in python?

Firstly is it possible to set a file's owner with python? And if so how do you set a file's owner with python?

like image 463
Jay Avatar asked May 30 '12 14:05

Jay


People also ask

How do you change ownership of a file in Python?

chown() method in Python is used to change the owner and group id of the specified path to the specified numeric owner id (UID) and group id (GID). Note: os. chown() method is available only on UNIX platforms and the functionality of this method is typically available only to the superuser or a privileged user.

How do you show the owner of a file in Python?

stat. It gives you st_uid which is the user ID of the owner. Then you have to convert it to the name. To do that, use pwd.

How do I change permissions on a file in Python?

Set file permissions (chmod) To change file permissions, you can use os. chmod(). You can bitwise OR the following options to set the permissions the way you want. These values come from the stat package: Python stat package documentation.


1 Answers

os.chown(path, uid, gid) 

http://docs.python.org/library/os.html

The uid and gid can be retrieved from a string by

import pwd import grp import os  uid = pwd.getpwnam("nobody").pw_uid gid = grp.getgrnam("nogroup").gr_gid 

Reference: How to change the user and group permissions for a directory, by name?

like image 112
Maria Zverina Avatar answered Sep 20 '22 17:09

Maria Zverina