Created
August 23, 2011 06:22
-
-
Save gjedeer/1164470 to your computer and use it in GitHub Desktop.
Change file owner user/group by either username/groupname or numeric ID
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import os | |
import pwd | |
import grp | |
def chown_by_name(path, user, group = None): | |
"""Chowns file based on username and groupname | |
If group name is left blank, group is preserved | |
""" | |
try: | |
uid = int(user) | |
except: | |
t = pwd.getpwnam(user) | |
uid = t[2] | |
if group == None: | |
gid = os.stat(path)[5] | |
else: | |
try: | |
gid = int(group) | |
except: | |
t = grp.getgrnam(group) | |
gid = t[2] | |
os.chown(path, uid, gid) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment