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 )
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER