[Tutor] Plz help me from this

Steve Willoughby steve at alchemy.com
Wed Sep 5 08:27:54 CEST 2007


chinni wrote:
> Hi All,
> 
> 
> I Want to write a python script to change the permissions(chmod) and 
> owner(chown) and group(chgrp) of a file on unix,
> script as to read from the /etc/passwd for displaying available users on 
> the machine and from /etc/groups it as to display the available groups 
> and user has to give the mode and owner name and group name from the list.
> 
> Can i implement this in python or in shell scripting
> Can any one please suggest me from doing this...:)

This would be a lot easier to do with Python than shell, although it's 
certainly possible to do in either.

For a start, check out the "nis" module in the standard library. 
nis.cat('passwd') and nis.cat('group') should be useful in getting the 
list of valid inputs.

 From there, you can create some sort of GUI or text-based selection 
code, or at least let the user type in the names they want and validate 
it against what's in NIS.

If your system doesn't use NIS, you can see the 'pwd', 'spwd' and 'grp' 
modules instead.  They'll get you what you need directly from the /etc 
files, but a little less conveniently.

Then once you have your input from the user, it's just a simple matter 
of calling os.chmod() and os.chown() to make the changes.  (Note that 
os.chown() changes the owner and/or group in one call.)

HTH
--steve




More information about the Tutor mailing list