Solved

changing owner (chown) programatically

Posted on 2004-10-07
2
1,250 Views
Last Modified: 2010-05-19
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 )
      
      
0
Comment
Question by:jesterepsilon
2 Comments
 
LVL 14

Accepted Solution

by:
RichieHindle earned 500 total points
ID: 12274992
The 'pwd' module maps between usernames and user IDs.

So to print the username rather than the user ID:

import pwd

# ...rest of your script...

def visitfile( file,mode ):
     userid = os.stat(file)[ST_UID]
     username = pwd.getpwuid(userid)[0]
     print 'file is %s and username is %s' % (file, username)

To go the other way, and get the user ID from a username:

     uid = pwd.getpwnam(username)[2]

I don't know about your last problem - for me, your script prints the user IDs of the subdirectories correctly.  I'm not clear about " I need to change the owner to the name of the directory" - do you need to change the owner of the directory itself, or all the files within it?  What about a case like '/tmp/user1/user2/file'?  What should happen to each component?

0
 

Author Comment

by:jesterepsilon
ID: 12304139
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).
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

"The time has come," the Walrus said, "To talk of many things: Of sets--and lists--and dictionaries-- Of variable kinks-- And why you see it changing not-- And why so strange are strings." This part describes how variables and references (see …
Installing Python 2.7.3 version on Windows operating system For installing Python first we need to download Python's latest version from URL" www.python.org " You can also get information on Python scripting language from the above mentioned we…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now