Solved

changing owner (chown) programatically

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Scrapy python: SSL handhshake failure  for GET request 1 513
need cgi page to refresh one time only after launch 11 149
windows 10 bash shell 4 48
linux crontab output 3 77
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…
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 strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
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…

860 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