Solved

changing owner (chown) programatically

Posted on 2004-10-07
2
1,264 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Variable is a place holder or reserved memory locations to store any value. Which means whenever we create a variable, indirectly we are reserving some space in the memory. The interpreter assigns or allocates some space in the memory based on the d…
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
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 …
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

733 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