changing owner (chown) programatically
Posted on 2004-10-07
OS X cmd line
I am using the following script to iterate through subdirectories and get the name of each subdirectory. I need to change the owner to the name of the directory. Using ListDir, I get the names of the directories. However, using os.stat, I get the user id instead of the string name. Is there a way to resolve this readily?
PS One other problem with this script is that it lists the user id of the top directory repeatedly (instead of the user id of the subdirectories). Observations/suggestions?
#example from python docs
from stat import *
'''recursively descend the directory tree rooted at top, calling the
callback function for eaach regular file'''
for f in os.listdir(top):
pathname = os.path.join(top,f)
mode = os.stat(pathname)[ST_MODE]
if S_ISDIR( mode ):
#it's a dir, recurse into it
#it's a file, call the callback fn
#unknown file type, print msg
print 'Skipping %s' % pathname
def visitfile( file,mode ):
userid = os.stat(file)[ST_UID]
print 'file is % s and userid is %s' % (file,userid)
if __name__ == '__main__':
walktree( sys.argv,visitfile )