Solved

changing owner (chown) programatically

Posted on 2004-10-07
2
1,258 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Strings in Python are the set of characters that, once defined, cannot be changed by any other method like replace. Even if we use the replace method it still does not modify the original string that we use, but just copies the string and then modif…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
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 while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

772 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