Link to home
Start Free TrialLog in
Avatar of jesterepsilon
jesterepsilon

asked on

changing owner (chown) programatically

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?  
Thanks,
je

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
#100704

import os,sys
from stat import *
def walktree(top,callback):
      '''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
                  walktree(pathname, callback)
            elif S_ISREG(mode):
                  #it's a file, call the callback fn
                  callback(pathname,mode)
            else:
                  #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[1],visitfile )
      
      
ASKER CERTIFIED SOLUTION
Avatar of RichieHindle
RichieHindle

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jesterepsilon
jesterepsilon

ASKER

Thanks for the response.  I'm going to try it out right now.  The 2nd problem was a mistake on my part.  The directory that I was reading was on a mapped drive, so the permissions on my mapping were all me (the userid that I connected with).